在iOS 9.0之前,Apple Spotlight僅能夠檢索iOS自身應用的内容,比如郵件、備忘錄、提醒、短信。第三方應用不支援被檢索,比如美團、大衆點評、今日頭條等等。在iOS9.0之後,iOS蘋果推出Search API,使得第三方APP内的頁面内容也可以被檢索。應用開發者按照Search API程式設計,然後使用者在Spotlight和Safari可以直接搜APP内的内容(In-App Search),這帶來很大的價值點。
據WWDC官方公布的使用者習慣資料,使用者86%的時間花在APP中,僅有14%的時間花在 Web上。是以APP有着較好的使用者體驗非常重要。
對APP開發者而言:
最大價值是提高APP的打開率,進而提高了APP的留存及活躍,提高APP的曝光度,使用者能夠更加友善的達到内容。
對使用者而言:
對于安裝很多APP的使用者,找個某一個APP,都特别困難。用Spotlight輸入APP的名字,便可找到。使用者在Spotlight也能夠友善查找大衆點評中的某一個餐廳。
Spotlight給我們提供了這樣好的功能,應用開發者怎樣使用呢?
**
iOS 9 Search API概述
**
•A private on-device index(私有裝置索引)。儲存在使用者裝置上,不會同步到伺服器與其它裝置上。
•Apple’s server-side index (Apple server索引)。pubulic index,内容儲存在應用伺服器。
**
Search API的三種用法
**
NSUserActivity
這個類我們很熟悉在iOS 8的時候,我們就拿它來做Handoff,在iOS 9中我們能拿它來做更多的事兒了~在這裡我們先說它的搜尋功能,當内容被記NSUserActivity,就可以在 Spotlight 和 Safari 中同時被搜尋到,現在這裡我們隻介紹建立使用者索引。
Core Spotlight
iOS 9中全新提出的Api,允許App在本地存一個類似索引的檔案,可以曾删改查,用來搜尋本地内容(on-device index)。适合持續的使用者資料。
Web markup。
網站上的内容如何在App中存在可以在搜尋顯示App相關資訊,pubulic index.内容必須在應用伺服器,蘋果通過applebot擷取相關資料,iOS所有使用者均可以利用Spotligight和Safari搜尋功能擷取到相關内容。(國内不支援)
**
為App添加Spotlight支援
**
建立了一個Demo工程做例子示範,最後會提供Demo下載下傳位址
-(IBAction)creatSearchableItem{
CSSearchableItemAttributeSet注:Spotlight隻支援iOS 9+如果你的項目支援iOS 9以下版本需要添加如下方法判斷
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
//code...
#endif
第一步:導入Framework
MobileCoreServices.framework
CoreSpotlight.framework
第二步:導入頭檔案
import <CoreSpotlight/CoreSpotlight.h>
import <MobileCoreServices/MobileCoreServices.h>
第三步:建立Spotlight索引
*attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
// 标題
attributeSet.title = @"标題";
// 關鍵字,NSArray可設定多個
attributeSet.keywords = @[@"demo",@"sp"];
// 描述
attributeSet.contentDescription = @"description";
// 圖示, NSData格式
attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"icon"]);
// Searchable item
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"1" domainIdentifier:@"linkedme.cc" attributeSet:attributeSet];
NSMutableArray *searchItems = [NSMutableArray arrayWithObjects:item, nil];
//indexSearchableItems 接收參數NSMutableArray
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchItems completionHandler:^(NSError * error) {
if (error) {
NSLog(@"索引建立失敗:%@",error.localizedDescription);
}else{
[self performSelectorOnMainThread:@selector(showAlert:) withObject:@"索引建立成功" waitUntilDone:NO];
}
}];
}
CSSearchableItemAttributeSet設定Spotlight搜尋内容的類,我們可以設定各種屬性如下圖
方法聲明
- (instancetype)initWithUniqueIdentifier:(NSString *)uniqueIdentifier
domainIdentifier:(NSString *)domainIdentifier
attributeSet:(CSSearchableItemAttributeSet *)attributeSet;
參數詳解
檢視官方文檔
通過上面的操作我們已經可以在Spotlight中搜尋到我們建立的索引内容了,可以搜尋到了下一步就是怎麼通過搜尋内容打開相應的頁面.
通過搜尋結果跳轉到相應頁面
在Appdelegate中添加下面方法
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
NSString* idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"]; //擷取傳入的索引資料的唯一辨別
if ([idetifier isEqualToString:@"1"]) {
DemoOneViewController * ovc = [[DemoOneViewController alloc]init];
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController pushViewController: ovc animated:true];
}
NSLog(@"%@",idetifier);
return YES;
}
同時Spotlight還提供删除索引方法,過期的索引需要手動删除,系統提供了三個删除索引方法
通過identifier删除索引
- (IBAction)deleteSearchableItemFormIdentifier{
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:@[@"1"] completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}else{
[self performSelectorOnMainThread:@selector(showAlert:) withObject:@"通過identifier删除索引成功" waitUntilDone:NO];
}
}];
}
通過DomainIdentifiers删除索引
- (IBAction)deleteSearchableItemFormDomain{
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@[@"linkedme.cc"] completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}else{
[self performSelectorOnMainThread:@selector(showAlert:) withObject:@"通過DomainIdentifiers删除索引成功" waitUntilDone:NO];
}
}];
}
删除所有索引
- (IBAction)deleteAllSearchableItem{
[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@",error.localizedDescription);
}else{
[self performSelectorOnMainThread:@selector(showAlert:) withObject:@"删除所有索引成功" waitUntilDone:NO];
}
}];
由以上步驟,移動開發者在開發APP時,可以內建Spotlight功能,但是在程式設計時,會遇到各種各樣的坑。內建Spotlight功能可以和深度連結結合,将大大降低開發成本,增強的深度連結也引導從管道(微信、微網誌、短信、郵件等)上一鍵喚醒APP。Spotlight和深度連結将怎樣更好的融合呢。請見《Core Spotlight和深度連結結合使用(下)》
下載下傳Demo
參考連接配接LinkedME