iOS 9 应用内搜索(CoreSpotlight)的使用

iOS9之前我们只能使用Spotlight来搜索应用名称来打开指定App,在iOS9以后Apple允许开发者设置应用中任意内容可以被Spotlight索引到。除打开APP还可以单独处理跳到特定页面。

在iOS9中提供了三种API来帮助我们实现搜索,如下:

NSUserActivity

NSUserActivity包含了一些新的方法和属性来帮助我们实现索引activities和应用状态使他们在搜索结果中可用。每一个应用都可以利用NSUserActivity API来生成对于用户来说更有用的内容。顺便提一句NSUserActivity在iOS8中的Handoff就已经被引入了。

Web Markup

这一特性允许应用镜像自己的内容,并在Spotlight中建立自己的引用。苹果的爬虫会抓取你的网站上打了markup的内容,而这些内容会提供给Safari和Spotlight。这个特性的神奇之处在于。用户不需要将你的应用安装在手机上。这样你的应用可以更多的展示给潜在用户。苹果的云服务会索引你的内容,让你的应用与Public Search API保持深度的链接会让你收益颇多。

Core Spotlight

新的CoreSpotlight(framework)是iOS9提供的一组新的API来帮助你建立起你的应用中的索引。CoreSpotlight是用来处理用户数据的比如:文档,照片以及其他类型的由用户产生的内容。

引入 CoreSpotlight.framewWork

#import <CoreSpotlight/CoreSpotlight.h> 

然后将需要索引的数据保存至CoreSpotlight

-(void)addSearchItemsArray:(NSArray *)array{

if(IS_IOS_9){ NSMutableArray <CSSearchableItem *> *searchableItems = [NSMutableArray arrayWithCapacity:0]; for (NSDictionary *d in array) { NSString *title = [d objectForKey:@"title"]; NSString *desc = [d objectForKey:@"desc"]; NSString *time = [d objectForKey:@"time"]; NSString *nid = [d objectForKey:@"nid"]; CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:@"SearchAPIViews"]; attributeSet.title = title; attributeSet.contentDescription = [NSString stringWithFormat:@"%@ %@",desc,time]; NSMutableArray *keywords = [NSMutableArray arrayWithArray:[title split:@" "]]; [keywords addObject:desc]; attributeSet.keywords = keywords; NSString *identifiner = [NSString stringWithFormat:@"%@",nid]; [searchableItems addObject:[[CSSearchableItem alloc]initWithUniqueIdentifier:identifiner domainIdentifier:IOS9SearchAPIUtil_domainIdentifier attributeSet:attributeSet]]; } [[CSSearchableIndex defaultSearchableIndex]indexSearchableItems:searchableItems completionHandler:^(NSError * __nullable error) { if(error != nil){ NSLog(@"%@",error.localizedDescription); }else { NSLog(@"Items were indexed successfully"); } }]; }
}

现在我们点击搜索到相应的项还只能打开我们的应用,如果要实现跳转还需要进行一小步的工作:在AppDelegate中实现

- (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{

NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

[navigationController popToRootViewControllerAnimated:YES];

//CoreSpotlightTableViewController *coreViewController = [[navigationController viewControllers] firstObject];

//[coreViewController showViewControllerWithIdentifier:idetifier];

return YES;

} 

最后需要提到的就是索引的删除。CoreSpotlight给我们提供了三个方法来进行删除分别是:

- (void)deleteSearchableItemsWithIdentifiers:(NSArray<NSString *> *)identifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler; 

- (void)deleteSearchableItemsWithDomainIdentifiers:(NSArray<NSString *> *)domainIdentifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler;

 - (void)deleteAllSearchableItemsWithCompletionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler; 
原文地址:https://www.cnblogs.com/KingQiangzi/p/4861851.html