1. 程式人生 > >iOS開發之整合iOS9中的Core Spotlight Framework搜尋App的內容

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的連結如下:

一.Demo執行效果

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

二.整合Core Spotlight Framework

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

2.在相應的檢視控制器中引入標頭檔案,然後就開始寫程式碼使自己的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陣列進行關聯。具體程式碼和步驟如下。

Objective-C
1234567891011121314151617181920212223242526272829303132333435363738394041 -(void)supportSpotlightSearch{dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);dispatch_async(queue,^{<ahref="http://www.jobbole.com/members/xyz937134366"data-mce-href="http://www.jobbole.com/members/xyz937134366">@try</a>{NSArray*temp=@[@"宮崎駿-龍貓",@"宮崎駿-千與千尋",@"宮崎駿-天空之城"];//建立SearchableItems的陣列NSMutableArray*searchableItems=[[NSMutableArrayalloc] initWithCapacity:temp.count];for(inti=0;i){//1.建立條目的屬性集合CSSearchableItemAttributeSet*attributeSet=[[CSSearchableItemAttributeSetalloc] initWithItemContentType:(NSString*)kUTTypeImage];//2.給屬性集合新增屬性attributeSet.title=temp[i];attributeSet.contentDescription=[NSString stringWithFormat:@"宮崎駿與%@",temp[i]];attributeSet.thumbnailData=UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]]);//3.屬性集合與條目進行關聯CSSearchableItem*searchableItem=[[CSSearchableItemalloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%d",i+1] domainIdentifier:@"ZeluLi.SpotlightSearchDemo" attributeSet:attributeSet];//把該條目進行暫存[searchableItems addObject:searchableItem];}//4.吧條目陣列與索引進行關聯[[CSSearchableIndexdefaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError*_Nullable error){if(!error){NSLog(@"%s, %@",__FUNCTION__,[error localizedDescription]);}}];}<ahref="http://www.jobbole.com/members/wx895846013"data-mce-href="http://www.jobbole.com/members/wx895846013">@catch</a>(NSException*exception){NSLog(@"%s, %@",__FUNCTION__,exception);}<ahref="http://www.jobbole.com/members/finally"data-mce-href="http://www.jobbole.com/members/finally">@finally</a>{}});}

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

Objective-C
1234567891011 -(BOOL)application:(nonnull UIApplication*)application continueUserActivity:(nonnull NSUserActivity*)userActivity restorationHandler:(nonnull void(^)(NSArray*__nullable))restorationHandler{NSString*idetifier=userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];UINavigationController*navigationController=(UINavigationController*)self.window.rootViewController;ViewController*vc=[navigationController viewControllers][0];[vc.myImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",idetifier]]];returnYES;}

打賞支援我寫出更多好文章,謝謝!

打賞作者

打賞支援我寫出更多好文章,謝謝!