iOS 9 Spotlight簡單使用

iOS9 支持應用內搜索,比如


點擊進入可以直接進入詳情界面。做到這點需要兩步:一、建立索引 2、點擊索引進入程序進行相應處理。


使用soptlight需要先導入靜態庫


 然後在需要建立索引的地方加入下面代碼

#import <CoreSpotlight/CoreSpotlight.h>

 

     NSMutableArray *seachableItems = [NSMutableArray new];
     //titles爲索引的標題數組,可以分別定義詳細內容和圖片,圖片爲空時,默認logo圖片
    [titles enumerateObjectsUsingBlock:^(NSString *__nonnull obj, NSUInteger idx, BOOL * __nonnull stop) {
        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"views"];
        attributeSet.title = obj;
        attributeSet.contentDescription = [NSString stringWithFormat:NSLocalizedString(@"快速打開 %@", nil),obj];
        UIImage *thumbImage = [UIImage imageNamed:[NSString stringWithFormat:@"icon_%@.png",obj]];
        attributeSet.thumbnailData = UIImagePNGRepresentation(thumbImage);//beta 1 there is a bug
        CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:obj   
         domainIdentifier:@"你的identifer"                                                                                                                                                                  attributeSet:attributeSet];
        [seachableItems addObject:item];
    }];
    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:seachableItems
                                                   completionHandler:^(NSError * __nullable error) {
                                                       if (!error)
                                                           NSLog(@"%@",error.localizedDescription);
                                                   }];



下面是通過點擊索引,應用被打開時調用的方法

- (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{
    NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];//這個是標題,通過標題可以知道點擊了什麼
    NSArray *arr = [[NSUserDefaults standardUserDefaults]objectForKey:@"homeDataArr"];
    for (NSDictionary *dict in arr){//遍歷數組找到點擊的標示進行處理,這個例子中根視圖是tabbar,具體代碼視情況而定
        NSString *subject = dict[@"subject"];
        if ([subject isEqualToString:idetifier]){
            UITabBarController *tabbar = (UITabBarController *)self.window.rootViewController;
            MPBaseNavViewController *nav = [[tabbar viewControllers] objectAtIndex:1];
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            MPHomeDetailViewController *editc = (MPHomeDetailViewController*)[story instantiateViewControllerWithIdentifier:@"MPHomeDetailVC"];
            [nav pushViewController:editc animated:YES];
            
            break;
        }
    }
   
    return YES;
    
} 

此外刪除索引的代碼

[[CSSearchableIndex defaultSearchableIndex]deleteSearchableItemsWithIdentifiers:@[idetifier] completionHandler:^(NSError * _Nullable error) {
 }];



發佈了61 篇原創文章 · 獲贊 2 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章