嗨我試圖在我的應用程序中實現CoreSpotlight。CoreSpotlight索引
索引編制時,我是否需要每次都運行它,或者是否足以在第一次安裝應用程序時運行一次? 如果應用程序被刪除,我需要再次索引?
下面是我使用的代碼:
- (void)spotLightIndexing {
NSString *path = [[NSBundle mainBundle] pathForResource:
@"aDetailed" ofType:@"plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray = [plistDict allKeys];
for (id key in plistDict) {
CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
// Set properties that describe attributes of the item such as title, description, and image.
attributeSet.title = key;
attributeSet.contentDescription = [plistDict objectForKey:key];
//*************************************
attributeSet.keywords = plistArray; // Another Q: do i need this????
//**************************************
// Create an attribute set for an item
UIImage *image = [UIImage imageNamed:@"icon.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;
// Create a searchable item, specifying its ID, associated domain, and the attribute set you created earlier.
CSSearchableItem *item;
NSString *identifier = [NSString stringWithFormat:@"%@",attributeSet.title];
item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:@"com.example.apple_sample.theapp.search" attributeSet:attributeSet];
// Index the item.
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
if (!error)
NSLog(@"Search item indexed");
else {
NSLog(@"******************* E R R O R *********************");
}];
}
}
謝謝
你的代碼工程?因爲當我把這段代碼放到我的項目中時,編譯器不會給出'搜索項目索引'輸出! –
@ Mc.Lover是的,它就像一個魅力!請參閱編輯答案以記錄任何錯誤 –
請您看看這個問題嗎? http://stackoverflow.com/questions/33443833/set-title-property-from-nsarray-in-cssearchableitemattributeset,我用你的代碼,但沒有工作! –