【发布时间】:2015-11-02 07:04:18
【问题描述】:
您好,我正在尝试在我的应用中实现 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 *********************");
}];
}
}
谢谢
【问题讨论】:
-
你的代码有效吗?因为当我把这段代码放在我的项目编译器上时,我没有
Search item indexed输出! -
@Mc.Lover 是的,它就像一个魅力!请参阅已编辑的答案以记录任何错误
-
请您看看这个问题好吗? stackoverflow.com/questions/33443833/…,我使用了你的代码,但是没有用!
标签: ios indexing corespotlight