仍是直插正题,有错误请指出。git
Spotlight这个功能很早就有了,可能大部分人以为这个东西没有什么用。可是若是应用装的不少的时候,好比你装了几百个应用,你很难快速的在桌面上找到你想要的应用。这个时候Spotlight就体现出做用了。iOS9苹果提供了更强大的应用内搜索功能,之前只能搜索到你的app,从iOS9开始你能够搜索到你应用内的某一个功能,用户经过搜索点击能够直接进入应用内的某一功能模块。。。算了仍是不墨迹了。。我这个文笔仍是很是醉人的,下面开始介绍这个spotlight的简单使用github
首先作了一些准备工做,建立项目拉等等, app
如上图作了几个简单界面用来作后面的跳转~~~分别设置几个颜色用于区分是哪个界面。不要问我为啥用这个几个颜色,,,由于我猜大家喜欢黄色。。。dom
firstView 为橙色view secondView为黄色viewide
接下来导入头文件 #import <CoreSpotlight/CoreSpotlight.h> 我是在appdelegate 里面导入的,而且在程序启动的时候初始化并设置搜索。测试
设置Spotlight的方法 *建立CSSearchableItemAttributeSet对象spa
CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"firstSet"];
复制代码
//标题 firstSet.title = @"测试firstView"; //详细描述 firstSet.contentDescription = @"测试firstView哈哈哈哈哈哈哈"; //关键字, NSArray *firstSeachKey = [NSArray arrayWithObjects:@"first",@"测试",@"firstView", nil]; firstSet.contactKeywords = firstSeachKey; 注:搜索界面显示的属性都是在这里设置的。展现内容,图片等等代理
*建立CSSearchableItem对象rest
CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"firstItem" domainIdentifier:@"first" attributeSet:firstSet];
复制代码
注:每一个set都要对应一个item。code
*设置搜索
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:itemArray completionHandler:^(NSError * _Nullable error) {
复制代码
if (error) { NSLog(@"设置失败%@",error);
}else{ NSLog(@"设置成功"); } }];
这个样子搜索就设置完了。经过你设置的关键字能够在Spotlight里搜索到你设置的内容,完整方法以下
- (void)setSpotlight{
复制代码
/应用内搜索,想搜索到多少个界面就要建立多少个set ,每一个set都要对应一个item/ CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"firstSet"]; //标题 firstSet.title = @"测试firstView"; //详细描述 firstSet.contentDescription = @"测试firstView哈哈哈哈哈哈哈"; //关键字, NSArray *firstSeachKey = [NSArray arrayWithObjects:@"first",@"测试",@"firstView", nil]; firstSet.contactKeywords = firstSeachKey; CSSearchableItemAttributeSet *secondSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"secondSet"]; secondSet.title = @"测试SecondView"; secondSet.contentDescription = @"测试secondView哈哈哈哈哈哈哈哈"; NSArray *secondArrayKey = [NSArray arrayWithObjects:@"second",@"测试",@"secondeVIew", nil]; secondSet.contactKeywords = secondArrayKey; //UniqueIdentifier每一个搜索都有一个惟一标示,当用户点击搜索到得某个内容的时候,系统会调用代理方法,会将这个惟一标示传给你,以便让你肯定是点击了哪一,方便作页面跳转 //domainIdentifier搜索域标识,删除条目的时候调用的delegate会传过来这个值 CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"firstItem" domainIdentifier:@"first" attributeSet:firstSet]; CSSearchableItem *secondItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"secondItem" domainIdentifier:@"second" attributeSet:secondSet]; NSArray *itemArray = [NSArray arrayWithObjects:firstItem,secondItem, nil]; [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:itemArray completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"设置失败%@",error);
}else{ NSLog(@"设置成功"); } }]; }
以后在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中调用
NSLog(@"%f",[UIDevice currentDevice].systemVersion.floatValue);
复制代码
if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) { [self setSpotlight]; } 如今设置搜索就设置完了。可是如今点击以后仍是只能进入应用不能进入指定的界面。还须要实现下面的代理,在代理方法里面作一些操做
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
复制代码
NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"]; NSLog(@"%@",idetifier); UINavigationController *nav = (UINavigationController *)self.window.rootViewController; UIStoryboard *stroy = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; if ([idetifier isEqualToString:@"firstItem"]) { FirstViewController *fistVc = [stroy instantiateViewControllerWithIdentifier:@"firstView"]; [nav pushViewController:fistVc animated:YES]; }else if ([idetifier isEqualToString:@"secondItem"]){ SecondViewController *secondVc = [stroy instantiateViewControllerWithIdentifier:@"secondView"]; [nav pushViewController:secondVc animated:YES]; } return YES; }
OK,所有完成~~~~系统还给提供了几个删除item的方法
- (void)deleteSearchableItemsWithIdentifiers:(NSArray<NSString *> *)identifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{
}
- (void)deleteSearchableItemsWithDomainIdentifiers:(NSArray<NSString *> *)domainIdentifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{
}
- (void)deleteAllSearchableItemsWithCompletionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{
}
复制代码