iOS 开发内容索引

- (void)setSpotlight{

/*应用内搜索,想搜索到多少个界面就要创建多少个set,每个set都要对应一个item*/

CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"firstSet"];

//标题

firstSet.title = @"测试firstView";

//详细描述

firstSet.contentDescription = @"测试firstView哈哈哈哈哈哈哈";

//关键字,

NSArray *firstSeachKey = [NSArray arrayWithObjects:@"first",@"测试",@"firstView", nil];

firstSet.contactKeywords = firstSeachKey;

NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"https://api.ykinfo.net/api/file/2475bbec499feec7f00fb891d810c9a7"]];

UIImage *image = [UIImage imageWithData:data];

CGSize size = CGSizeMake(100, 100);

UIGraphicsBeginImageContext(size);

//绘制改变大小的图片

[image drawInRect:CGRectMake(0,0, size.width, size.height)];

//从当前context中创建一个改变大小后的图片

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

//使当前的context出堆栈

UIGraphicsEndImageContext();

firstSet.thumbnailData = UIImageJPEGRepresentation(scaledImage, 0.5);

CSSearchableItemAttributeSet *secondSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"secondSet"];

secondSet.title = @"测试SecondView";

secondSet.contentDescription = @"测试secondView哈哈哈哈哈哈哈哈";

NSArray *secondArrayKey = [NSArray arrayWithObjects:@"second",@"测试",@"secondeVIew", nil];

secondSet.contactKeywords = secondArrayKey;

//UniqueIdentifier每个搜索都有一个唯一标示,当用户点击搜索到得某个内容的时候,系统会调用代理方法,会将这个唯一标示传给你,以便让你确定是点击了哪一,方便做页面跳转

//domainIdentifier搜索域标识,删除条目的时候调用的delegate会传过来这个值

CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"firstItem" domainIdentifier:@"first" attributeSet:firstSet];

CSSearchableItem *secondItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"secondItem" domainIdentifier:@"second" attributeSet:secondSet];

NSArray *itemArray = [NSArray arrayWithObjects:firstItem,secondItem, nil];

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:itemArray completionHandler:^(NSError * _Nullable error) {

if (error) {

NSLog(@"设置失败%@",error);

}else{

NSLog(@"设置成功");

}

}];

}

你可能感兴趣的:(iOS 开发内容索引)