gpt4 book ai didi

ios - CoreSpotlight 索引

转载 作者:可可西里 更新时间:2023-11-01 03:56:49 26 4
gpt4 key购买 nike

您好,我正在尝试在我的应用中实现 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 *********************");


}];

}
}

谢谢

最佳答案

它按指定索引。因此,如果您将 spotLightIndexing 方法放在 didFinishLaunchingWithOptions 中,它会在每次启动时自然地为项目编制索引,当然,除非您设置了 bool 值。如果应用程序被删除,它将再次重新索引,因为 NSUserDefault 值将被清零。这就是为什么他们通过批量更新或其他方法为您提供添加/更改/更新索引的原因,如注释 here

由于您是从本地 plist 而不是 Web 填充它,因此您必须自己进行更新或创建索引维护应用程序扩展。

如果您观看有关此主题的 WWDC 视频,您会发现使用域标识符的“组”可以轻松更新或删除域。 Source这是一 block 好表。

至于关键字,在文档完全支持 iOS9 API 之前是无从知晓的。但只要阅读 Apple 在这里公开提供的内容,您就应该考虑一下:

Important: Be sure to avoid over-indexing your app content or adding unrelated keywords and attributes in an attempt to improve the ranking of your results. Because iOS measures the level of user engagement with search results, items that users don’t find useful are quickly identified and can eventually stop showing up in results.

它位于新的搜索功能摘要之后。它继续说明原因:

When you combine multiple Search APIs, items can get indexed from multiple places. To avoid giving users duplicate items in search results, you need to link item IDs appropriately. To ensure that item IDs are linked, you can use the same value in a searchable item’s uniqueIdentifier property and in the relatedUniqueIdentifier property within an NSUserActivity object’s contentAttributes property

换句话说,假设您按照他们的意愿合并了 NSUserActivity,因为它可以应用于您应用的所有用户,而不仅仅是进行查询的人,它可以在同一搜索中多次填充。因此,根据 Apple 的建议,除非您确定,否则请尽量不要使用关键字,尤其是根据您的示例,其中关键字已经 = uniqueIdentifier。

就我个人而言,我已经在我的应用程序中实现了它并且很喜欢它,但是,我使用网络标记几乎可以即时进行批量更新,而不是你的路线,你必须实际推出新的更新重新更新/删除索引。

关于ios - CoreSpotlight 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31355749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com