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