iOS9新特性 - Search APIs
在IOS9之前,你只能用spotlight通過app的名字找到對應的app。但隨著iOS9 Search APIs 的發布,開發人員可以通過在app內部的內容中建立索引,通過搜尋索引鍵來進入到app指定的內容地區。
The 3 APIs
NSUserActivity
The NSUserActivity 在iOS8的 Handoff 使用中有介紹到。到了iOS9中,你可以利用NSUserActivity搜尋相關的“活躍資訊”。你可以提供一些關鍵詞給這些“活躍資訊”, 意味著spotlight可以檢索它們。這個操作相當於你在瀏覽網頁的時候,有記錄的作用一樣。使用者可以通過spotlight快速的開啟最近的“活躍資訊”。
Web Markup
Web Markup 允許 apps 映射它們的內容到網頁上面,然後方便spotlight在網頁中快速檢索內容。Apple的索引器將會扮演爬蟲的角色,在網頁上面檢索那些被Markup的內容。這些資訊在Safari和Spotlight都可以檢索。
Core Spotlight
Core Spotlight是iOS9中的一個新架構,它允許檢索在app中的內容。NSUserActivity在儲存使用者的記錄是很有用的;而Core Spotlight可以檢索任何你想要的資料。
使用Core Spotlight APIs
NSUserActivity 和 Web Markup APIs 相對而言是很容易使用的,而 Core Spotlight 是有一點複雜的。為了示範Core Spotlight是怎樣工作的,我們建立一個比較簡單的Demo用來展示朋友列表。然後你點擊任意一個朋友的名字,你可以看到朋友的頭像的具體資訊。在示範具體的流程之前,我們先看看最終。
在示範效果中可以看到,我在spotlight中檢索相關朋友資訊,可以看到他們的大致資訊,然後點擊一條資訊,便可以跳轉到自己app中的具體的朋友詳情。
程式碼分析:
1. Demo的結構很簡單,就是一個導航控制器,根控制器是一個UITableViewController,用來展示朋友名稱列表。我們定義為FriendTableViewController。
2. 當點擊列表中的一個朋友名稱後,就會跳轉到詳情頁面,我們定義為FriendViewController。
3. 所有的朋友資料資訊,我們用一個管理類來管理。我們定義為DataSource。這個管理類的職責:
1)儲存所有的朋友資料資訊。
2)儲存使用者資訊到Core Spotlight的索引器中。
當然,每一條使用者資訊對應一個模型,定義如下:
Person.h
@interface Person : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *identifer;@property (nonatomic, copy) NSString *icon;- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon;@end
Person.m
@implementation Person- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon { if (self = [super init]) { self.name = name; self.identifer = identifer; self.icon = icon; } return self;}@end
DataSource.h的方法列表如下:
@interface Datasource : NSObject- (NSArray *)dataList;- (Person *)findFriendWithId:(NSString *)identifer;- (void)savePeopleToIndex;@end
1)dataList方法就是擷取所有的使用者列表資料資訊。
2)findFriendWithId: 方法就是根據使用者Id擷取模型資料。
3)savePeopleToIndex就是儲存所有使用者資料資訊到Core Spotlight的索引器中。
DataSource.m 檔案中的代碼
@implementation Datasource- (NSArray *)dataList { Person *becky = [[Person alloc] initWithName:@Becky identifer:@1 icon:@becky]; Person *ben = [[Person alloc] initWithName:@Ben identifer:@2 icon:@ben]; Person *jane = [[Person alloc] initWithName:@Jane identifer:@3 icon:@jane]; Person *pete = [[Person alloc] initWithName:@Pete identifer:@4 icon:@pete]; Person *ray = [[Person alloc] initWithName:@Ray identifer:@5 icon:@ray]; Person *tom = [[Person alloc] initWithName:@Tom identifer:@6 icon:@tom]; return @[becky, ben, jane, pete, ray, tom];}- (Person *)findFriendWithId:(NSString *)identifer { for (Person *p in self.dataList) { if ([p.identifer isEqualToString:identifer]) { return p; } } return nil;}- (void)savePeopleToIndex { // prepare NSMutableArray *searchableItems = [NSMutableArray array]; for (Person *p in self.dataList) { // Create an attribute set for an item that represents an image. CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@image]; attributeSet.title = p.name; attributeSet.contentDescription = [NSString stringWithFormat:@This is an entry all about the interesting person called %@, p.name]; attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:p.icon]); CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:p.identifer domainIdentifier:@com.ios9daybyday.SearchAPIs.people attributeSet:attributeSet]; [searchableItems addObject:item]; } // save [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@error message:%@, error.localizedDescription); } }];}@end
代碼的關鍵區段就是savePeopleToIndex方法,定義的searchableItems就是用來儲存相關的可檢索的資訊;而代碼中的CSSearchableIndex的單例方法indexSearchableItems是真正的將searchableItems中的內容儲存到Core Spotlight中的操作。
然後我們看看FriendTableViewController的列表展示頁面的主要代碼
- (void)viewDidLoad { [super viewDidLoad]; Datasource *dataSource = [Datasource alloc]; self.dataList = [dataSource dataList]; [dataSource savePeopleToIndex];}
這裡的savePeopleIndex就將內容儲存到Core Spotlight中了。
現在你運行程式,這些資料將會被儲存起來了。當你在spotlight中搜尋你的朋友,他們將會出現,如下:
此時嘗試驗擊一項,但是它不會跳轉到app的指定地區,只會跳轉到對應的app,因為我們還沒有指定要跳轉的指定地區。
我們可以在調用continueUserActivity代理方法的時候指定app的行為。代碼如下:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler NS_AVAILABLE_IOS(8_0) { NSString *friendID = userActivity.userInfo[@kCSSearchableItemActivityIdentifier]; UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; [navigationController popToRootViewControllerAnimated:NO]; FriendTableViewController *friendVC = (FriendTableViewController *)navigationController.viewControllers.firstObject; [friendVC showFriendWithId:friendID]; return YES;}
第1句代碼: 擷取你在spotlight中點擊的朋友的id。
第2、3句代碼: 擷取到根的導航控制,並且pop掉棧中所有的控制器。
第4句代碼: 跳轉到app的指定位置。
這裡的代碼可以可以發現,之前我們儲存到Core SpotLight索引器中的內容現在可以使用userActivity.userInfo字典進行擷取了。我們所關心的就是friend Id,它是被儲存到索引器中作為Person這個對象的kCSSearchableItemActivityIdentifier.
正如你所見的,左上方有一個 Back to search選項,使用者可以點擊這裡返回使用者列表。
這篇文章中,並沒有涉及到索引器的刪除操作,想要瞭解刪除的具體操作,可以參照以下的幾個方法:
deleteSearchableItemsWithIdentifiersdeleteSearchableItemsWithDomainIdentifiersdeleteAllSearchableItemsWithCompletionHandler