1.Spotloight是什么?

  Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlight的App可以在Spotlight中搜索App的内容,并且通过内容打开相关页面。

  Demo演示

  

2.如何集成Spotlight

  a.添加所需要的框架 

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
#import <CoreSpotlight/CoreSpotlight.h>
#import <MobileCoreServices/MobileCoreServices.h>
#endif

  注,很多APP都是支持iOS9以下的,因此加入#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000,可以解决iOS9以下设备运行崩溃的问题

  b.创建 CSSearchableItemAttributeSet 对象

CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];attributeSet.title = spotlightTitle;                // 标题attributeSet.keywords = keywords;                   // 关键字,NSArray格式attributeSet.contentDescription = spotlightDesc;    // 描述attributeSet.thumbnailData = photo;                 // 图标, NSData格式

  // 把图片转换成NSData的方法  UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]

  c.创建可检索条目CSSearchableItem

// spotlightInfo 可以作为一些数据传递给接受的地方
// domainId      id,通过这个id来判断是哪个spotlight
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:spotlightInfo domainIdentifier:domainId attributeSet:attributeSet];

  d.添加检索入口

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) {if (error) {NSLog(@"indexSearchableItems Error:%@",error.localizedDescription);}
}];

  ========完整代码========

- (void)insertSearchableItem:(NSData *)photo spotlightTitle:(NSString *)spotlightTitle description:(NSString *)spotlightDesc keywords:(NSArray *)keywords spotlightInfo:(NSString *)spotlightInfo domainId:(NSString *)domainId {CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];attributeSet.title = spotlightTitle;                // 标题attributeSet.keywords = keywords;                   // 关键字,NSArray格式attributeSet.contentDescription = spotlightDesc;    // 描述attributeSet.thumbnailData = photo;                 // 图标, NSData格式// spotlightInfo 可以作为一些数据传递给接受的地方// domainId      id,通过这个id来判断是哪个spotlightCSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:spotlightInfo domainIdentifier:domainId attributeSet:attributeSet];[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) {if (error) {NSLog(@"indexSearchableItems Error:%@",error.localizedDescription);}}];
}

  ========加载本地图片的使用方法========

[self insertSearchableItem:UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]) spotlightTitle:@"等风来" description:@"等风来描述" keywords:@[@"鲍鲸鲸",@"大丽花"] spotlightInfo:@"传递过去的值" domainId:@"com.wb.spotlight"];

  ========加载网络图片的使用方法========

 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://hiphotos.baidu.com/doc/pic/item/eaf81a4c510fd9f905f61934262dd42a2934a48e.jpg"]];[self insertSearchableItem:data spotlightTitle:@"等风来" description:@"等风来描述" keywords:@[@"鲍鲸鲸",@"大丽花"] spotlightInfo:@"传递过去的值" domainId:@"com.wb.spotlight"];});

  ========删除所有spotlight的方法========

[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {if (error) {NSLog(@"%@", error.localizedDescription);}}];

  ========删除指定的spotlight的方法========

[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@"domainId" completionHandler:^(NSError * _Nullable error) {if (error) {NSLog(@"%@", error.localizedDescription);}}];

  ========点击spotlight后的响应方法========

 

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) {NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier];// 接受事先定义好的数值,如果是多个参数可以使用把json转成string传递过来,接受后把string在转换为jsonNSLog(@"传递过来的值%@", uniqueIdentifier);}return YES;
}

  备注:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000  // 相关spotlight的方法等#endif

// Spotlight支持iOS9以上设备运行,对与低版本的设备需加入这个防止崩溃问题

转载于:https://www.cnblogs.com/jgCho/p/4961435.html

iOS9 开发新特性 Spotlight使用相关推荐

  1. Windows 7 开发新特性

    10月25日在西安举行的Windows 7 社区发布活动中我讲了Session1 -- Windows 7 概览.参会的人员达到62人,这个参加人数超过了我的预期,非常开心. 主要讲了一下内容: 一 ...

  2. iOS9的新特性以及适配方案

    1. 限制HTTP协议,全部改用更安全的HTTPS iOS9让所有的HTTP默认使用了HTTPS,原来的HTTP协议传输都改成TLS1.2协议进行传输.直接造成的情况就是App发请求的时候弹出网络无法 ...

  3. Visual Studio 2013 Web开发新特性

    微软正式发布Visual Studio 2013 RTM版,微软还发布了Visual Studio 2013的最终版本..NET 4.5.1以及Team Foundation Server 2013. ...

  4. 【干货】iOS9的新特性UI Tests

    视频汇总首页:http://edu.51cto.com/lecturer/index/user_id-4626073.html 什么是UI Tests呢? UI Tests是苹果提供给我们进行UI测试 ...

  5. [AX]AX2012开发新特性-全文索引

    全文索引在索引中识别字符串字段中使用空格隔开的单词,不像普通索引仅仅使用字符串的第一个单词,这样能加快对字符串字段的搜索.AX2012支持每个表有一个且只能有一个全文索引,索引可以包含多个字段,对字段 ...

  6. SQL Server 2012 开发新特性:文件表

    转载于 春哥 一.启用FileTable 1.修改数据库引擎的属性 打开"SQL Server配置管理器",修改SQL Server数据库引擎的属性.使用此页可针对此 Micros ...

  7. iOS开发 - 新特性的坑之应用字体根据系统字体大小而变化

    iOS10之后,只要应用加入了相应代码就可以让应用的字体大小和系统同步,诸如此类的说明在网上横行,各种介绍,但却很少有给出实例的,有人问我怎么设,说没反应,我试了下并没发现什么,看了对方的代码,原来是 ...

  8. iOS 新特性实现 3DTouch 开发

    好久没有写过文章了,今天就给大家带来一篇比较简单的开发教程吧!目的是为了让大伙能够避开那些不必要的坑,快速高效的实现功能.今天呢来和大家讲一下iOS9的新特性3DTouch开发(iOS10都已经出了B ...

  9. ios9和ios10的新特性

    ios9和ios10的新特性 昨天面试了一个做ios开发的公司,其中面试官问我最新的ios系统版本是多少,以及它的特性是什么?由于自己是初学者,所以对这些没有关注过.今天特地搜索了一下关于ios9和i ...

最新文章

  1. 假设训练数据集中有10万个词,四元语法需要存储多少词频和多词相邻频率?《动手学深度学习 李沐》 转
  2. powerpoint打字慢
  3. JAVA模拟某信网登录信息采集
  4. thinkphp phpexcel导出
  5. Wannafly模拟赛2
  6. 【完整版-斯坦福-密码学】全13讲(绪论)【笔记】
  7. Hibernate 添加数据 二 (多对多)
  8. 阿里云ECS利用密钥对ssh登录服务器
  9. IIS搭配Server-u构建企业空间服务(二)
  10. java制作云图,Python爬虫 制作词云图
  11. 数组中相同元素 java_如何在一维数组中查找某重复元素,并输出其位置?用java实现...
  12. 【2019杭电多校第六场1011=HDU6644】11 Dimensions(dp+思维)
  13. 足球大数据预测胜平负、走地之人工智能算法现状与改进措施
  14. 360开源的插件化框架Replugin深度剖析
  15. 矩阵A的值域空间和其零空间
  16. react中定时器的应用_在离子React电容器应用中实施admob
  17. dataframe排序 pd.rank()
  18. uni-app引入阿里图标
  19. 如何利用Simulink来设计一个AEB的算法,并通过SCANeR仿真来测试?
  20. 查看linux 系统 服务器型号

热门文章

  1. 如何在Eclipse中如何自动添加注释和自定义注释风格
  2. 细胞亚器互作之细胞代谢调节液泡与线粒体的接触位Cellular Metabolism Regulates Contact Sites between Vacuoles and Mitochondria
  3. 米家扫地机器人重置网络_谁家扫地机器人悄声打扫更干净,科沃斯DEEBOT N3对比米家1S...
  4. 利用Python进行数据分析--绘图和可视化
  5. 计算机用户名SYSTEN,system是什么用户
  6. 文字识别(五)--自然场景文本检测技术综述(CTPN, SegLink, EAST)
  7. 使命召唤为啥显示新服务器玩家,《使命召唤8》建立服务器和加入自己、别人服务器方法...
  8. mysql集群 hbase_hbase完整分布式集群搭建
  9. 详解窗口句柄,进程ID,进程句柄,窗口与进程之间的关系
  10. 自定义Json解析工具