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来判断是哪个spotlight
CSSearchableItem *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, ), ^{
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在转换为json
NSLog(@"传递过来的值%@", uniqueIdentifier);
}
return YES;
}

  备注:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
  // 相关spotlight的方法等
#endif // Spotlight支持iOS9以上设备运行,对与低版本的设备需加入这个防止崩溃问题

  如果你不是在wb145230博客园看到本文,请点击查看原文

 

iOS9 Spotlight使用的更多相关文章

  1. iOS开发之集成iOS9中的Core Spotlight Framework搜索App的内容

    Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotlight.集成Spotlight的 ...

  2. iOS9 Search API 之 Spotlight

    iOS9以后 有三种api提供搜搜方式 加强引导用户关注 我们的app及相关内容的方式 NSUserActivity Web Markup  Core Spotlight 用法 前两种 实战操作性不够 ...

  3. iOS9 开发新特性 Spotlight使用

    1.Spotloight是什么? Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotl ...

  4. What's New in iOS9 iOS9功能改进

    What's New in iOS9 This article summarizes the key developer-related features introduced in iOS 9, w ...

  5. iOS 9 Spotlight搜索 OC版

    介绍:    在WWDC 2015会议上,苹果官方公布了iOS9.除开许多新的特性和增强功能,这次升级也给了开发者们一个机会让他们的app里的内容能通过Spotlight 搜索功能被发现和使用.在iO ...

  6. Swift基础之如何使用iOS 9的Core Spotlight框架

    本文由CocoaChina译者KingOfOnePiece(博客)翻译 作者:GABRIEL THEODOROPOULOS?校对:hyhSuper 原文:How To Use Core Spotlig ...

  7. iOS7,iOS8和iOS9的区别

    iOS7,iOS8和iOS9的区别:iOS7.0 1.iOS 7是iOS面世以来在用户界面上做出改变最大的一个操作系统.iOS 7抛弃了以往的拟物化设计,而采用了扁平化设计. 苹果在重新思考 iOS ...

  8. iOS9支付宝无法调起客户端

    1.为了适配 iOS9.0 中的 App Transport Security(ATS)对 http 的限制,这里需要对 支付宝的请求地址 alipay.com 做例外,在 app 对应的 info. ...

  9. 【转】 iOS9.2-iOS9.3.3越狱插件清单

    以下是iOS9.3.3越狱插件清单 原文地址:http://bbs.feng.com/read-htm-tid-10668605.html 序列 支持与否 插件名称 兼容版本 支持设备 1 是 20 ...

随机推荐

  1. [Angular] Updating and resetting FormGroups and FormControls

    If you want to reset or just update field value, you can do: reset: reset({key: value, k2:v2}); upda ...

  2. JAVA类(下)

    我看完了Java类,与C++相比,复杂了一点.其中有类的嵌套定义即内部类,枚举类等. 我看这两节花了我很多时间.其中有一些概念还是有点难懂. 下面,我详细总结内部类与枚举类. 内部类 内部类的主要作用 ...

  3. Nginx与真实IP

    配置了Nginx,Tomcat中的Web程序,获得的ip一直是"127.0.0.1",比较纳闷.获得远程ip,已经判断了很多情况,为什么会这样呢? 正解 proxy_set_hea ...

  4. Oracle数据库零散知识09 -- DBLink的创建(转)

    通过创建database link实现Oracle跨数据库查询的方法 在Oracle本地数据库端执行赋权dbuser帐号 SQL> grant create database link to d ...

  5. 自绘实现半透明水晶按钮(继承CButton,设置BS_OWNERDRAW风格,覆盖DrawItem函数绘制按钮,把父窗口的背景复制到按钮上,实现视觉上的透明,最后通过AlphaBlend实现半透明)

    运行效果 实现方法 1.给按钮加上BS_OWNERDRAW样式2.重载DrawItem函数,在这里绘制按钮3.关键之处就是把父窗口的背景复制到按钮上,实现视觉上的透明4.最后通过AlphaBlend实 ...

  6. erlang app 文件

    http://hje.iteye.com/blog/1211734 应用的概念¶ 当我们写了实现特定功能的代码之后,我们可能想将代码转成一个 应用 (application),这是可以作为一个单元启动 ...

  7. 【a603】加工生产调度

    Time Limit: 1 second Memory Limit: 32 MB [问题描述] 某工厂收到了n个产品的订单,这n个产品分别在A.B两个车间加工,并且必须先在A车间加工后才可以到B车间加 ...

  8. hdu3698 Let the light guide us dp+线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  9. go-wingui 2018 全新 v2.0 版本发布,包含重大更新!

    go-wingui 2018 全新 v2.0 版本发布,包含重大更新!使用新版CEF内核Chromium 63.0.3239.109,页面可以使用最新的css3,html5技术.使用delphi7重写 ...

  10. 阿里云CentOS7系统搭建JavaWeb环境

    一,准备工作 1,安装目录 我们创建如下路径/usr/develop,然后在develop目录下面创建java,tomcat和mysql三个目录即可. 二,配置JDK 1.理解wget命令 wget命 ...