当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS9开放的新API--Spotlight使用指南

iOS9开放的新API--Spotlight使用指南

2019年07月24日  | 移动技术网移动技术  | 我要评论
1.spotloight是什么?   spotlight在ios9上做了一些新的改进, 也就是开放了一些新的api, 通过core spotlight frame

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, 0), ^{
        nsdata * data = [nsdata datawithcontentsofurl:[nsurl urlwithstring:@""]];
        [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以上设备运行,对与低版本的设备需加入这个防止崩溃问题

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网