3

我正在尝试同时使用NSUserActivity,Core Spotlight APIs和 web 标记。

所以基于App Search Programming Guide中给出的建议

如果您使用所有三个 API,则最好使用相关网页的 URL 作为 uniqueIdentifier、relatedUniqueIdentifier 和 pagesURL 的值。

这就是我创建项目的方式:

NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"com.test.product"];
activity.title = self.detailModel.title;
activity.userInfo = @{ @"id" : self.detailModel.productId };
activity.eligibleForHandoff = NO;
activity.eligibleForPublicIndexing = YES;
activity.eligibleForSearch = YES;
activity.webpageURL = [NSURL URLWithString:[self shareUrl]];

activity.contentAttributeSet.contentDescription = self.detailModel.proDesc;
activity.contentAttributeSet.title = self.detailModel.title;
activity.contentAttributeSet.thumbnailData = UIImagePNGRepresentation(image);
activity.contentAttributeSet.relatedUniqueIdentifier = [self shareUrl];

CSSearchableItemAttributeSet *attrSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeURL];
attrSet.title = self.detailModel.title;
attrSet.contentDescription = self.detailModel.proDesc;
attrSet.thumbnailData = activity.contentAttributeSet.thumbnailData;
attrSet.relatedUniqueIdentifier = [self shareUrl];

CSSearchableItem *searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:[self shareUrl] domainIdentifier:@"com.test.product" attributeSet:attrSet];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[ searchableItem ] completionHandler:^(NSError *_Nullable error) {
    if (error) {
        DDLogError(@"%@", error.localizedDescription);
    } else {
        DDLogInfo(@"Core Spotlight indexed");
    }
  }];
  }
}];

self.systemUserActivity = activity;
[self.systemUserActivity becomeCurrent];

但是当我尝试在聚光灯下搜索时,有两个问题:

  • 出现重复的项目,一个来自活动,另一个来自聚光灯索引

  • 这两个项目只显示应用程序图标而不是我设置的图像

4

0 回答 0