
代碼需要運行在ios9或更高版本上
需要增加系統版本的判斷
#pragma mark - Searchable搜索
+(void)addSearchableItem{
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"duwu.Topic.test"];
attributeSet.title = @"title";
attributeSet.contentDescription = @"content";
//試了一下傳URL進去 好像是顯示不出網絡的圖片
// attributeSet.thumbnailURL = [NSURL URLWithString:@"imageURL"];
attributeSet.keywords = @[@"可以",@"增加",@"多個",@"關鍵詞"];
//因為我沒本地圖片去顯示 而且加到Searchable搜索裡好像也不用多實時 我便采用了子線程下載圖片等圖片下載完了在增加到搜索欄裡的方法
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *_thumbnailData = [[NSData alloc] initWithContentsOfURL:attributeSet.thumbnailURL];
attributeSet.thumbnailData = _thumbnailData;
CSSearchableItem *_item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"一個特殊的ID,便於用戶點擊進入APP後區別處理" domainIdentifier:@"分組信息" attributeSet:attributeSet];
//刪除某個分組的Searchable搜索
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@[@"分組信息"] completionHandler:^(NSError * _Nullable error) {
}];
//增加信息到Searchable搜索
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[_item] completionHandler:^(NSError * _Nullable error) {
}];
});
}最好也不要增加過多的關鍵詞和信息 濫用的話應該會被系統把權重降低的...