iOS開發之整合iOS9中的Core Spotlight Framework搜尋App的內容,ios9spotlight

來源:互聯網
上載者:User

iOS開發之整合iOS9中的Core Spotlight Framework搜尋App的內容,ios9spotlight

  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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.