必须是iOS9.0之上的系统,需要导入CoreSpotlight.framework框架。
CSSearchableItemAttributeSet
CSSearchableItem
CSSearchableIndex
先移除,再添加新的索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*移除所有sohu索引*/
-(void) removeFromIndexByDomain{
if([[UIDevice currentDevice] deviceSystemMajorVersion] < 9){
return;
}
NSArray* deleteIdentify = @[@"MyIdentify"];
[[CSSearchableIndex defaultSearchableIndex ]deleteSearchableItemsWithDomainIdentifiers:deleteIdentify completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@ %@",NSStringFromSelector(_cmd),error);
}
}];
}

/*将数据写入系统spotLight索引*/
-(void) saveSpotLightItemToIndex:(SpotLightItem*)spotLightItem thumbnailData:(NSData*)thumbnailData{
if([[UIDevice currentDevice] deviceSystemMajorVersion] < 9){
return;
}
/*从索引中移除*/
NSArray* deleteIdentify = @[spotLightItem.indentify];
[[CSSearchableIndex defaultSearchableIndex ]deleteSearchableItemsWithIdentifiers:deleteIdentify completionHandler:^(NSError * _Nullable error) {
if (error) {
DDLogWarn(@"deleteSearchableItemsWithIdentifiers: %@",error);
}
}];
CSSearchableItemAttributeSet* oneSet = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:(NSString*)kUTTypeAudiovisualContent];
CSSearchableItem * item = [[CSSearchableItem alloc]initWithUniqueIdentifier:spotLightItem.indentify domainIdentifier:SOHUSpotLightDomain attributeSet:oneSet];
oneSet.ratingDescription = @"搜狐视频";
oneSet.performers = @[@"金秀贤",@"成龙"];
oneSet.information = @"搜狐视频0";
oneSet.playCount = @(100);
oneSet.rating = @(4);
oneSet.comment = @"comment";
oneSet.director = @"director";
oneSet.producer = @"producer";
oneSet.copyright = @"copyright";
/*添加到系统索引中*/
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * __nullable error) {
//log
}];