Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlight的App可以在Spotlight中搜索App的内容,并且通过内容打开相关页面。因为接到开发任务,老大说让在App中支持Spotlight, 于是又搞了搞苹果的官方文档。可以说,集成Spotlight不算复杂,官网上讲的也挺明白的,今天博客就通过一个Demo来集成一下Spotlight。

  苹果官方有关Core Spotlight Framework的链接如下:

  https://developer.apple.com/library/prerelease/ios/documentation/CoreSpotlight/Reference/CoreSpotlight_Framework/index.html#//apple_ref/doc/uid/TP40016250

  一.Demo运行效果

  还是通过一个Demo来进行介绍,Demo运行效果如下。我们App中有关于宫崎骏的的内容,然后在Spotlight中搜索宫崎骏,就可以搜索到相关内容,并且可以点击打开展示相关内容。具体运行效果如下:

  二.集成Core Spotlight Framework

    1.想在App中使用Spotlight,首先得引入Core Spotlight Framework,Targets ->General -> linked Frameworks and Libraries 点击加号添加CoreSpotlight.framework。如下截图所示。

    2.在相应的视图控制器中引入<CoreSpotlight/CoreSpotlight.h>头文件,然后就开始写代码使自己的App内容支持Spotlight搜索了。下面是为Demo添加Spotlight的相关代码。Spotlight搜索出来的东西,每一项就是一个条目即CSSearchableItem的对象,而改对象又关联一个属性集合(CSSearchableItemAttributeSet )该集合中存储了CSSearchableItem对象的相关属性,如果title(标题), contentDescription(内容简介),

thumbnailData(图片)等所需内容。具体请看下方代码描述和代码注释。

    代码描述:

      (1).首先定义了一个temp数组,用来存储在Spotlight中搜索的关键字,也就是Spotlight可以搜索到的App内容。数组中的内容通过循环遍历经过一系列的步骤给Spotlight进行关联。

      (2)在每次遍历内容数组的过程中,需要创建一个CSSearchableItemAttributeSet(属性集合),并给属性集合中的一些属性赋上值。然后再创建一个CSSearchableItem,创建CSSearchableItem时,把其对应的属性集合进行关联。把每次创建好的条目暂存到可变数组中,因为创建好所有的条目后还要和Spotlight的索引(CSSearchableIndex)进行关联。

      (3)通过单例获取CSSearchableIndex的对象,并与我们创建好的CSSearchableItem数组进行关联。具体代码和步骤如下。

 - (void)supportSpotlightSearch {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
dispatch_async(queue, ^{
@try {
NSArray *temp = @[@"宫崎骏-龙猫", @"宫崎骏-千与千寻", @"宫崎骏-天空之城"]; //创建SearchableItems的数组
NSMutableArray *searchableItems = [[NSMutableArray alloc] initWithCapacity:temp.count]; for (int i = ; i < temp.count; i ++) { //1.创建条目的属性集合
CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*) kUTTypeImage]; //2.给属性集合添加属性
attributeSet.title = temp[i];
attributeSet.contentDescription = [NSString stringWithFormat:@"宫崎骏与%@", temp[i]];
attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+]]); //3.属性集合与条目进行关联
CSSearchableItem *searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%d", i+] domainIdentifier:@"ZeluLi.SpotlightSearchDemo" attributeSet:attributeSet]; //把该条目进行暂存
[searchableItems addObject:searchableItem];
} //4.吧条目数组与索引进行关联
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"%s, %@", __FUNCTION__, [error localizedDescription]);
}
}];
}
@catch (NSException *exception) {
NSLog(@"%s, %@", __FUNCTION__, exception);
}
@finally { }
});
}

    3.处理搜索后条目点击的事件,该事件的处理要在AppDelegate中下面的委托代理方法中进行处理。下面的idetifier就是属性集合与条目进行关联时指定的唯一标示。

 - (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{

     NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];

     UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

     ViewController *vc = [navigationController viewControllers][];
[vc.myImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",idetifier]]]; return YES;
}

    DEMO分享地址--github:https://github.com/lizelu/SpotlightSearchDemo

最新文章

  1. Java开发11个过不去的梗
  2. Android背景渐变色(shape,gradient)
  3. DNS配置
  4. A/B 测试之前必须要了解的 10 件事
  5. MS SQL Server之光标、存储过程和触发器
  6. Web前端开发规范文档你需要知道的事--HTML、css、js、文档等需要规范内容
  7. jQuery 遍历函数(w3school)
  8. mongodb 几个要注意的问题
  9. Linux 文件与目录
  10. .config-20150410
  11. codevs 3269 混合背包
  12. js怪招(摘录篇)
  13. (9)Xamarin测试账号申请与续用
  14. Android Studio 插件开发详解三:翻译插件实战
  15. javascript this 的工作原理
  16. vue 双向数据绑定原理
  17. ARM指令学习
  18. java集合类学习
  19. jdk1.8源码Synchronized及其实现原理
  20. ubuntu安装Anaconda2-4.4.0+TensorFlow

热门文章

  1. 用ProxyFactoryBean创建AOP代理
  2. java-sql注入攻击
  3. Odoo 二次开发教程(三)-第一个Model及Form、Tree视图
  4. 应用程序无法正常启动0xc0150002(windows server 2003)
  5. tomcat:域名指向项目名
  6. angularjs服务-service
  7. SQL——行值表达式(Row Value Expressions)
  8. SSH配置与讲解
  9. Java获得键盘输入的两种方法
  10. ajax优点与缺点