4

I'm trying to using NSUserActivity, Core Spotlight APIs and web markup at the same time.

So based on the suggestions given in App Search Programming Guide

If you’re using all three APIs, it works well to use the URL of the relevant webpage as the value for uniqueIdentifier, relatedUniqueIdentifier, and webpageURL.

This is how I created the item:

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];

But when I tried to search in spotlight, there are two problems:

  • Duplicated items show up, one from activity and the other from spotlight index

  • Both items only show with the app icon instead of the image I set

xi.lin
  • 3,326
  • 2
  • 31
  • 57

0 Answers0