iOS開發之集成iOS9中的Core Spotlight Framework搜索App的內容


  Spotlight在iOS9上做了一些新的改進, 也就是開放了一些新的API, 通過Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlight的App可以在Spotlight中搜索App的內容,並且通過內容打開相關頁面。因為接到開發任務,老大說讓在App中支持Spotlight, 於是又搞了搞蘋果的官方文檔。可以說,集成Spotlight不算復雜,官網上講的也挺明白的,今天博客就通過一個Demo來集成一下Spotlight。

  蘋果官方有關Core Spotlight Framework的鏈接如下:

  https://developer.apple.com/library/prerelease/ios/documentation/CoreSpotlight/Reference/CoreSpotlight_Framework/index.html#//apple_ref/doc/uid/TP40016250

  一.Demo運行效果

  還是通過一個Demo來進行介紹,Demo運行效果如下。我們App中有關於宮崎駿的的內容,然后在Spotlight中搜索宮崎駿,就可以搜索到相關內容,並且可以點擊打開展示相關內容。具體運行效果如下:

  二.集成Core Spotlight Framework

    1.想在App中使用Spotlight,首先得引入Core Spotlight Framework,Targets ->General -> linked Frameworks and Libraries 點擊加號添加CoreSpotlight.framework。如下截圖所示。

 

    2.在相應的視圖控制器中引入<CoreSpotlight/CoreSpotlight.h>頭文件,然后就開始寫代碼使自己的App內容支持Spotlight搜索了。下面是為Demo添加Spotlight的相關代碼。Spotlight搜索出來的東西,每一項就是一個條目即CSSearchableItem的對象,而改對象又關聯一個屬性集合(CSSearchableItemAttributeSet )該集合中存儲了CSSearchableItem對象的相關屬性,如果title(標題), contentDescription(內容簡介),

thumbnailData(圖片)等所需內容。具體請看下方代碼描述和代碼注釋。

    代碼描述:

      (1).首先定義了一個temp數組,用來存儲在Spotlight中搜索的關鍵字,也就是Spotlight可以搜索到的App內容。數組中的內容通過循環遍歷經過一系列的步驟給Spotlight進行關聯。

      (2)在每次遍歷內容數組的過程中,需要創建一個CSSearchableItemAttributeSet(屬性集合),並給屬性集合中的一些屬性賦上值。然后再創建一個CSSearchableItem,創建CSSearchableItem時,把其對應的屬性集合進行關聯。把每次創建好的條目暫存到可變數組中,因為創建好所有的條目后還要和Spotlight的索引(CSSearchableIndex)進行關聯。

      (3)通過單例獲取CSSearchableIndex的對象,並與我們創建好的CSSearchableItem數組進行關聯。具體代碼和步驟如下。

 1 - (void)supportSpotlightSearch {
 2     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 3     dispatch_async(queue, ^{
 4         @try {
 5             NSArray *temp = @[@"宮崎駿-龍貓", @"宮崎駿-千與千尋", @"宮崎駿-天空之城"];
 6             
 7             //創建SearchableItems的數組
 8             NSMutableArray *searchableItems = [[NSMutableArray alloc] initWithCapacity:temp.count];
 9             
10             for (int i = 0; i < temp.count; i ++) {
11                 
12                 //1.創建條目的屬性集合
13                 CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*) kUTTypeImage];
14                 
15                 //2.給屬性集合添加屬性
16                 attributeSet.title = temp[i];
17                 attributeSet.contentDescription = [NSString stringWithFormat:@"宮崎駿與%@", temp[i]];
18                 attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]]);
19                 
20                 //3.屬性集合與條目進行關聯
21                 CSSearchableItem *searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%d", i+1] domainIdentifier:@"ZeluLi.SpotlightSearchDemo" attributeSet:attributeSet];
22                 
23                 //把該條目進行暫存
24                 [searchableItems addObject:searchableItem];
25             }
26             
27             //4.吧條目數組與索引進行關聯
28             [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
29                 if (!error) {
30                     NSLog(@"%s, %@", __FUNCTION__, [error localizedDescription]);
31                 }
32             }];
33         }
34         @catch (NSException *exception) {
35             NSLog(@"%s, %@", __FUNCTION__, exception);
36         }
37         @finally {
38             
39         }
40     });
41 }

 

    3.處理搜索后條目點擊的事件,該事件的處理要在AppDelegate中下面的委托代理方法中進行處理。下面的idetifier就是屬性集合與條目進行關聯時指定的唯一標示。

 1 - (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{
 2     
 3     NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
 4     
 5     UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
 6     
 7     ViewController *vc = [navigationController viewControllers][0];
 8     [vc.myImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",idetifier]]];
 9     
10     
11     return YES;
12 }

 

    DEMO分享地址--github:https://github.com/lizelu/SpotlightSearchDemo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM