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


1.Spotloight是什么?

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

  Demo演示

  

2.如何集成Spotlight

  a.添加所需要的框架 

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以上设备运行,对与低版本的设备需加入这个防止崩溃问题


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3