iOS9中增加了許多新特性(What's New in iOS 9.0 - Apple Developer),其中的Search功能讓我高度興趣。簡單看了一下,主要有三種方式,來實現local和server兩種搜尋。
自己動手嘗試了下第二種,即用Core Spotlight架構來實現Index App Content。主要步驟如:
1. 添加所需要的架構
需要兩個framework:CoreSpotlight和MobileCoreServices,可以直接@import(有關@import和#import的區別:@import vs #import),這樣就不用在項目中手工添加framework。
@import CoreSpotlight;@import MobileCoreServices;
2. 建立 CSSearchableItemAttributeSet
CSSearchableItemAttributeSet *myAttributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeData]; // 搜尋結果中顯示的標題 myAttributeSet.title = @"Apple"; // 搜尋結果中顯示的詳情 myAttributeSet.contentDescription = @"A red apple"; // 關鍵詞,當使用者輸入以下字串時,相關內容會被檢索到 myAttributeSet.keywords = [NSArray arrayWithObjects:@"Hello", @"Search", @"Fruit", nil];
3. 建立可檢索條目 CSSearchableItem
// 初始化一個可檢索的條目 CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"apple-001" domainIdentifier:@"xxx.fruit.com" attributeSet:myAttributeSet];
4. 添加檢索入口
// 添加入口 [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"Search item failed to be indexed"); } else { NSLog(@"Search item indexed"); } }];
5. 刪除檢索入口 有三種方式來刪除檢索入口,分別是根據uniqueID,domain以及刪除所有入口
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:[NSArray arrayWithObjects:@"apple-001", @"apple-002", nil] completionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"Items deleted"); } }]; [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:[NSArray arrayWithObjects:@"xxx.fruit.com", nil] completionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"Items deleted"); } }]; [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"All items deleted"); } }];
6. Full example
//// FirstViewController.m// Hello Search#import "FirstViewController.h"@import CoreSpotlight;@import MobileCoreServices;@interface FirstViewController ()@end@implementation FirstViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initSearchableIndex];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)initSearchableIndex { CSSearchableItemAttributeSet *myAttributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeData]; // 搜尋結果中顯示的標題 myAttributeSet.title = @"Apple"; // 搜尋結果中顯示的詳情 myAttributeSet.contentDescription = @"A red apple"; // 關鍵詞,當使用者輸入以下字串時,相關內容會被檢索到 myAttributeSet.keywords = [NSArray arrayWithObjects:@"Hello", @"Search", @"Fruit", nil]; // 初始化一個可檢索的條目 CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"apple-001" domainIdentifier:@"xxx.fruit.com" attributeSet:myAttributeSet]; // 添加入口 [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"Search item failed to be indexed"); } else { NSLog(@"Search item indexed"); } }];}- (void)deleteSearchableIndex { [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:[NSArray arrayWithObjects:@"apple-001", @"apple-002", nil] completionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"Items deleted"); } }]; [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:[NSArray arrayWithObjects:@"xxx.fruit.com", nil] completionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"Items deleted"); } }]; [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"All items deleted"); } }];}@end