需要判斷設(shè)備系統(tǒng)版本是否為iOS9及以上并導(dǎo)入框架
CoreSpotlight/CoreSpotlight.hMobile 和 CoreServices/MobileCoreServices.h
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
#import <>
#import
#endif
/**
往iOS 9以上設(shè)備Spotlight中增加條目
@param photo圖片數(shù)據(jù)轉(zhuǎn)化成二進(jìn)制NSDatae.g.UIImagePNGRepresentation([UIImage imageNamed:@"DIY.jpg"])
@param spotlightTitle搜索結(jié)果標(biāo)題e.g.@"頁zi"
@param spotlightDesc搜索結(jié)果詳細(xì)描述e.g.@"你要找的是不是XXX"
@param keywords搜索關(guān)鍵字NSString組成的NSAtrray e.g.@[@"女神",@"胁⑴冢花"]
@param spotlightInfo傳遞過去的值补君,可以作為一些數(shù)據(jù)傳遞給接受的地方
@param domainIdid,通過這個(gè)id來判斷是哪個(gè)spotlight
*/
- (void)insertSearchableItem:(NSData*)photo spotlightTitle:(NSString*)spotlightTitle description:(NSString*)spotlightDesc keywords:(NSArray*)keywords spotlightInfo:(NSString*)spotlightInfo domainId:(NSString*)domainId {
//
CSSearchableItemAttributeSet*attributeSet = [[CSSearchableItemAttributeSetalloc]initWithItemContentType:(NSString*)kUTTypeImage];
attributeSet.title= spotlightTitle;//標(biāo)題
attributeSet.keywords= keywords;//關(guān)鍵字,NSArray格式
attributeSet.contentDescription= spotlightDesc;//描述
attributeSet.thumbnailData= photo;//圖標(biāo), NSData格式
//
CSSearchableItem*item = [[CSSearchableItemalloc]initWithUniqueIdentifier:spotlightInfo
domainIdentifier:domainIdattributeSet:attributeSet];
[[CSSearchableIndexdefaultSearchableIndex]indexSearchableItems:@[item]
completionHandler:^(NSError* error) {
if(error) {
NSLog(@"indexSearchableItems Error:%@",error.localizedDescription);
}
}];
}