星空网 > 软件开发 > 操作系统

iOS9 开发新特性 Spotlight使用

   

1.Spotloight是什么?

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

  Demo演示

  iOS9 开发新特性 Spotlight使用images/loading.gif' data-original="http://images2015.cnblogs.com/blog/423172/201511/423172-20151112201913494-1959089873.gif" />

 

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 对象

iOS9 开发新特性 Spotlight使用
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"]
iOS9 开发新特性 Spotlight使用

  c.创建可检索条目CSSearchableItem

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

  d.添加检索入口

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

 

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

iOS9 开发新特性 Spotlight使用
- (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);          }  }];}
iOS9 开发新特性 Spotlight使用

 

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

 

[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后的响应方法========

 

iOS9 开发新特性 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;}
iOS9 开发新特性 Spotlight使用

 

  备注:

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

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




原标题:iOS9 开发新特性 Spotlight使用

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

新西兰/The Realm of New Zealand/新西兰王国:https://www.kjdsnews.com/a/1837914.html
新西兰/The Realm of New Zealand/新西兰王国:https://www.xlkjsw.com/news/72974.html
最新调查:TikTok用户使用时长趋于稳定,用户新增放缓:https://www.kjdsnews.com/a/1837915.html
最新调查:TikTok用户使用时长趋于稳定,用户新增放缓:https://www.goluckyvip.com/news/204214.html
物流业的内卷“新宠”,为何是它?:https://www.kjdsnews.com/a/1837916.html
物流业的内卷“新宠”,为何是它?:https://www.xlkjsw.com/news/72975.html
instagram竞品“TikTok Notes”在加拿大和澳大利亚上线:https://www.goluckyvip.com/news/220211.html
青岛黄岛景点门票一览 - 青岛黄岛旅游景点介绍免费:https://www.vstour.cn/a/410226.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流