點擊下載 Demo
一、Core Spotlight 簡介
iOS9 推出了 Core Spotlight 框架,這個框架可以為 iOS 的搜索 App 內(nèi)部的數(shù)據(jù),能夠使我們在 iPhone 上下拉出現(xiàn)得搜索框中,搜索我們使用的 App 里面的內(nèi)容。它創(chuàng)建的索引存儲在設(shè)備上,不與Apple共享,也不能被其他應(yīng)用或者設(shè)備訪問。
Apple的指南中特別提到Core Spotlight創(chuàng)建的索引最好在幾千的數(shù)量級別之下。索引太多很有可能會帶來性能問題。

二、添加索引
1、引用 spotlight 頭文件
在需要的地方引用以下頭文件:
#import <CoreSpotlight/CoreSpotlight.h>
2、創(chuàng)建 CSSearchableItemAttributeSet 對象
應(yīng)用內(nèi)搜索,想搜索到多少個界面就要創(chuàng)建多少個set ,每個set都要對應(yīng)一個 CSSearchableItem 對象。
CSSearchableItemAttributeSet *set =
[[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"ContentType"];
然后就可以根據(jù)需求設(shè)置不同的搜索樣式圖,效果如上圖所示。下面列取幾種常用設(shè)置
基本展示:
set.title = @"基本展示";
set.contentDescription = @"我是基本展示詳情,包含標(biāo)題和詳情描述。這里沒設(shè)置圖片,默認(rèn)展示 App icon";
// 搜索關(guān)鍵詞務(wù)必要設(shè)置,不然會搜索不到
set.contactKeywords = @[@"我是", @"基本", @"展示"];
圖片與星評:
set.title = @"圖片與星評";
set.contentDescription = @"我是圖片與星評展示,設(shè)置的圖片展示的效果系統(tǒng)并不會給你處理,設(shè)置什么樣的圖片就會展示什么樣的圖片。還可以設(shè)置星評";
set.contactKeywords = @[@"我是", @"圖片", @"星評"];
set.rating = @(3.5);
set.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"share_qq"]);
導(dǎo)航設(shè)置:
set.title = @"導(dǎo)航設(shè)置";
set.contentDescription = @"我是導(dǎo)航設(shè)置,點擊導(dǎo)航會跳轉(zhuǎn)到地圖,然后系統(tǒng)自動導(dǎo)航";
set.latitude = @(31.239);
set.longitude = @(121.499);
set.supportsNavigation = @(YES);
set.contactKeywords = @[@"我是", @"導(dǎo)航", @"設(shè)置"];
撥打電話:
set.title = @"撥打電話";
set.contentDescription = @"我是撥打電話樣式,只在真機上有效,模擬器無效";
set.phoneNumbers = @[@"10086"];
set.supportsPhoneCall = @(YES);
set.contactKeywords = @[@"我是", @"撥打", @"電話"];
3、創(chuàng)建 CSSearchableItem 對象
CSSearchableItem *item =
[[CSSearchableItem alloc] initWithUniqueIdentifier:@"uniqueIdentifier"
domainIdentifier:@"domainIdentifier"
attributeSet:attributeSet];
uniqueIdentifier 每個搜索都有一個唯一標(biāo)示,當(dāng)用戶點擊搜索到得某個內(nèi)容的時候,系統(tǒng)會調(diào)用代理方法,會將這個唯一標(biāo)示傳給你,以便讓你確定是點擊了哪一,方便做頁面跳轉(zhuǎn)。
domainIdentifier 搜索域標(biāo)識,刪除條目的時候調(diào)用的delegate會傳過來這個值。
attributeSet 就是剛才我們創(chuàng)建的 CSSearchableItemAttributeSet 對象。
4、添加 CSSearchableIndex 中
接下來是調(diào)用 Core Spotlight 相應(yīng)的 API 來對數(shù)據(jù)進行索引操作,將前兩步創(chuàng)建完成的 CSSearchableItem 對象,加入 CSSearchableIndex 中,該方法可以對索引進行添加或更新。
// 把上面的設(shè)置item都添加進入
NSArray *items = @[item1, item2, item3, item4];
CSSearchableIndex *searchIndex = [CSSearchableIndex defaultSearchableIndex];
[searchIndex indexSearchableItems:items
completionHandler:^(NSError * _Nullable error) {
if (!error) { // 回調(diào)方法在子線程
NSLog(@"添加成功");
} else {
NSLog(@"添加失敗%@",error);
}
}];
三、點擊索引
在 AppDelegate.m 文件中, 我們需要實現(xiàn)一個代理方法,用來響應(yīng)搜索結(jié)果。 這個代理方法將會在 Spotlight 搜索結(jié)果點擊后被調(diào)用,在這里,我們可以處理被點擊的索引。
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
NSString *idetifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];
NSLog(@"idetifier : %@",idetifier);
NSString *activityType = userActivity.activityType;
NSLog(@"activityType : %@",activityType);
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor whiteColor];
// 根據(jù) activityType、idetifier 識別索引
if ([idetifier isEqualToString:@"baseId"]) {
vc.title = @"基本展示";
} else if ([idetifier isEqualToString:@"iconId"]){
vc.title = @"圖片與星評";
} else if ([idetifier isEqualToString:@"navigationId"]) {
vc.title = @"導(dǎo)航設(shè)置";
} else if ([idetifier isEqualToString:@"phoneId"]) {
vc.title = @"撥打電話";
}
if (vc.title) {
[nav pushViewController:vc animated:YES];
}
return YES;
}
四、刪除索引
CSSearchableIndex類提供了三個方法來刪除索引,分別刪除應(yīng)用所創(chuàng)建的所有索引,按domain ID刪除索引,按ID刪除索引。
- deleteAllSearchableItemsWithCompletionHandler:
- deleteSearchableItemsWithDomainIdentifiers:completionHandler:
- deleteSearchableItemsWithIdentifiers:completionHandler:
根據(jù) UniqueIdentifier 刪除索引示例:
- (IBAction)deleteSearchItem {
// 根據(jù) UniqueIdentifier 刪除索引
NSArray *identifiers = @[@"navigationId", @"phoneId"];
CSSearchableIndex *searchIndex = [CSSearchableIndex defaultSearchableIndex];
[searchIndex deleteSearchableItemsWithIdentifiers:identifiers
completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"刪除成功");
} else {
NSLog(@"刪除失敗%@",error);
}
}];
}
五、注意事項
1、搜索結(jié)果的位置和順序是系統(tǒng)自動排布的,大致是點擊頻率越高就越靠前
2、同一個應(yīng)用,搜索結(jié)果列表默認(rèn)展示3個,然后展開后最多展示13個
3、批量添加過多(測試:1000條索引,每個索引10個關(guān)鍵詞),會阻塞線程,導(dǎo)致卡頓
4、可以不設(shè)置 thumbnailData ,這樣默認(rèn)就是當(dāng)前應(yīng)用的 LOGO;另外注意 thumbnailURL 并不是用來指定網(wǎng)絡(luò)圖片地址的,所以你要加縮略圖的話需要先把網(wǎng)上的下載到本地再設(shè)置。設(shè)置 thumbnailURL 時,如果顯示不出圖片,可以拼接前綴 file:// 然后重試