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

2020-01-14 17:32:24丽君

  UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]

 

 

  c.创建可检索条目CSSearchableItem

 

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

 

  d.添加检索入口

 

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

 

 

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

 

复制代码
- (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];