仍是直插正題,有錯誤請指出。git
Spotlight這個功能很早就有了,可能大部分人以爲這個東西沒有什麼用。可是若是應用裝的不少的時候,好比你裝了幾百個應用,你很難快速的在桌面上找到你想要的應用。這個時候Spotlight就體現出做用了。iOS9蘋果提供了更強大的應用內搜索功能,之前只能搜索到你的app,從iOS9開始你能夠搜索到你應用內的某一個功能,用戶經過搜索點擊能夠直接進入應用內的某一功能模塊。。。算了仍是不墨跡了。。我這個文筆仍是很是醉人的,下面開始介紹這個spotlight的簡單使用github
首先作了一些準備工做,建立項目拉等等, app
如上圖作了幾個簡單界面用來作後面的跳轉~~~分別設置幾個顏色用於區分是哪個界面。不要問我爲啥用這個幾個顏色,,,由於我猜大家喜歡黃色。。。dom
firstView 爲橙色view secondView爲黃色viewide
接下來導入頭文件 #import <CoreSpotlight/CoreSpotlight.h> 我是在appdelegate 裏面導入的,而且在程序啓動的時候初始化並設置搜索。測試
設置Spotlight的方法 *建立CSSearchableItemAttributeSet對象spa
CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"firstSet"];
複製代碼
//標題 firstSet.title = @"測試firstView"; //詳細描述 firstSet.contentDescription = @"測試firstView哈哈哈哈哈哈哈"; //關鍵字, NSArray *firstSeachKey = [NSArray arrayWithObjects:@"first",@"測試",@"firstView", nil]; firstSet.contactKeywords = firstSeachKey; 注:搜索界面顯示的屬性都是在這裏設置的。展現內容,圖片等等代理
*建立CSSearchableItem對象rest
CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"firstItem" domainIdentifier:@"first" attributeSet:firstSet];
複製代碼
注:每一個set都要對應一個item。code
*設置搜索
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:itemArray completionHandler:^(NSError * _Nullable error) {
複製代碼
if (error) { NSLog(@"設置失敗%@",error);
}else{ NSLog(@"設置成功"); } }];
這個樣子搜索就設置完了。經過你設置的關鍵字能夠在Spotlight裏搜索到你設置的內容,完整方法以下
- (void)setSpotlight{
複製代碼
/應用內搜索,想搜索到多少個界面就要建立多少個set ,每一個set都要對應一個item/ CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"firstSet"]; //標題 firstSet.title = @"測試firstView"; //詳細描述 firstSet.contentDescription = @"測試firstView哈哈哈哈哈哈哈"; //關鍵字, NSArray *firstSeachKey = [NSArray arrayWithObjects:@"first",@"測試",@"firstView", nil]; firstSet.contactKeywords = firstSeachKey; CSSearchableItemAttributeSet *secondSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"secondSet"]; secondSet.title = @"測試SecondView"; secondSet.contentDescription = @"測試secondView哈哈哈哈哈哈哈哈"; NSArray *secondArrayKey = [NSArray arrayWithObjects:@"second",@"測試",@"secondeVIew", nil]; secondSet.contactKeywords = secondArrayKey; //UniqueIdentifier每一個搜索都有一個惟一標示,當用戶點擊搜索到得某個內容的時候,系統會調用代理方法,會將這個惟一標示傳給你,以便讓你肯定是點擊了哪一,方便作頁面跳轉 //domainIdentifier搜索域標識,刪除條目的時候調用的delegate會傳過來這個值 CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"firstItem" domainIdentifier:@"first" attributeSet:firstSet]; CSSearchableItem *secondItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"secondItem" domainIdentifier:@"second" attributeSet:secondSet]; NSArray *itemArray = [NSArray arrayWithObjects:firstItem,secondItem, nil]; [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:itemArray completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"設置失敗%@",error);
}else{ NSLog(@"設置成功"); } }]; }
以後在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中調用
NSLog(@"%f",[UIDevice currentDevice].systemVersion.floatValue);
複製代碼
if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) { [self setSpotlight]; } 如今設置搜索就設置完了。可是如今點擊以後仍是隻能進入應用不能進入指定的界面。還須要實現下面的代理,在代理方法裏面作一些操做
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
複製代碼
NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"]; NSLog(@"%@",idetifier); UINavigationController *nav = (UINavigationController *)self.window.rootViewController; UIStoryboard *stroy = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; if ([idetifier isEqualToString:@"firstItem"]) { FirstViewController *fistVc = [stroy instantiateViewControllerWithIdentifier:@"firstView"]; [nav pushViewController:fistVc animated:YES]; }else if ([idetifier isEqualToString:@"secondItem"]){ SecondViewController *secondVc = [stroy instantiateViewControllerWithIdentifier:@"secondView"]; [nav pushViewController:secondVc animated:YES]; } return YES; }
OK,所有完成~~~~系統還給提供了幾個刪除item的方法
- (void)deleteSearchableItemsWithIdentifiers:(NSArray<NSString *> *)identifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{
}
- (void)deleteSearchableItemsWithDomainIdentifiers:(NSArray<NSString *> *)domainIdentifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{
}
- (void)deleteAllSearchableItemsWithCompletionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{
}
複製代碼