何为App Search?在iOS9之前,用户通过spotlight只能搜索app的名字,或者苹果自带app的一些内容,比如搜索通讯录、备忘录中的内容。iOS9开始,用户可以通过spotlight搜索网页内容或者任何app内的数据。
如图,网上找的.
有空再加注释
- (void)spotlight{
CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"firstSet"];
firstSet.title = @"主人,世上只有主人好";
firstSet.contentDescription = @"主人不要冷落我,点点我嘛";
firstSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"没有"]);
NSArray * firstSearchKey = @[@"直播",@"美女",@"QQ"];
firstSet.contactKeywords = firstSearchKey;
CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"firstItem" domainIdentifier:@"first" attributeSet:firstSet];
CSSearchableItemAttributeSet *secondSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"secondSet"];
secondSet.title = @"bibi求宠幸";
secondSet.contentDescription = @"主人,点我有惊喜呦";
secondSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"没有"]);
NSArray *secondKey = @[@"微信",@"视频",@"支付宝"];
secondSet.contactKeywords = secondKey;
CSSearchableItem *secondItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"secondItem" domainIdentifier:@"second" attributeSet:secondSet];
NSArray *items = @[firstItem,secondItem];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:items completionHandler:^(NSError * _Nullable error) {
if (error) {
DLog(@"%@,搜索设置失败");
}else{
DLog(@"搜索设置成功");
}
}];
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
NSString *idetifier = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
NSLog(@"%@搜索标识符",idetifier);
if ([idetifier isEqualToString:@"firstItem"]) {
DLog(@"点击111%@",self.window.rootViewController);
DLog(@"%@",[self getsCurrentVC]);
}else if ([idetifier isEqualToString:@"secondItem"]){
DLog(@"点击222");
DLog(@"%@",[self getsCurrentVC]);
}
return YES;
}