嗨,我正在尝试在我的应用程序中实现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 *********************");
}];
}
}谢谢
发布于 2015-07-14 22:57:07
它按指定的索引。因此,如果您将spotLightIndexing方法放入didFinishLaunchingWithOptions中,那么它将自然地在每次启动时对条目进行索引,当然,除非您设置了一个bool。如果该应用程序被删除,它将再次重新索引,因为NSUserDefault值将被归零。这就是为什么它们通过批处理更新或其他方法(如带注释的这里 )向您提供添加/更改/更新索引的原因。
由于您从本地plist填充它,而不是web,您将不得不自己进行更新或创建一个索引维护应用程序扩展。
如果您观看有关此主题的WWDC视频,您将看到使用域标识符通过“组”更新或删除域很容易。来源,这是块好表。
至于关键词,直到文档完全支持iOS9 API,才能知道。不过,只要阅读一下苹果公开提供的信息,你就应该考虑一下:
重要:确保避免过度索引您的应用程序内容或添加无关的关键字和属性,以努力提高您的排名结果。因为iOS测量用户对搜索结果的参与程度,所以用户认为不有用的项很快就会被识别出来,并最终会在结果中停止显示。
它位于新搜索功能摘要之后。它接着又说了为什么:
组合多个搜索API时,可以从多个位置对项进行索引。为了避免在搜索结果中给用户提供重复的项,您需要适当地链接项ID。为了确保链接项in,可以在可搜索项的uniqueIdentifier属性中和在NSUserActivity对象的contentAttributes属性中的relatedUniqueIdentifier属性中使用相同的值
换句话说,假设您按照他们的意愿合并了NSUserActivity,因为它可以应用于应用程序的所有用户,而不仅仅是进行查询的人,它可以在相同的搜索中多次填充。因此,根据Apples建议,尽量不要使用关键字,除非您的确定(特别是基于您的示例)关键字已经= uniqueIdentifier。
就我个人而言,我已经在我的应用程序中实现了这一点,而且我很喜欢它,然而,我使用了web标记,它使批处理更新几乎是即时的,而不是您的路径,在这个路径中,您必须推出一个新的更新来重新更新/删除索引。
https://stackoverflow.com/questions/31355749
复制相似问题