Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which properties should be used with CSSearchableItemAttributeSet on NSUserActivity?

I added Web Markup to my website so items will appear in Spotlight Search results when users search in iOS 9. Users can browse the same items in the app, therefore I want to create NSUserActivity objects that link to the web content as users browse the items.

Now, NSUserActivity has a contentAttributeSet property which I will use to to attach a thumbnail photo to the activity. CSSearchableItemAttributeSet has some properties that NSUserActivity also has, so I am not sure which one I should implement or if I should specify the same data for both. Do I set the title for the NSUserActivity as well as the title on the CSSearchableItemAttributeSet, or only one or the other? Same with keywords which is a property on both as well.

NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@“com.domain.appname-something"];
activity.title = @“My Title";
activity.keywords = [NSSet setWithArray:@[@“one", @“two", @“three"]];
activity.userInfo = @{@“id": @“12345"};
activity.requiredUserInfoKeys = [NSSet setWithArray:@[@“id"]];
activity.eligibleForSearch = YES;
activity.eligibleForPublicIndexing = YES;
activity.webpageURL = [NSURL URLWithString:@"https://someurl.com"];

//QUESTION: Do I need to duplicate title and keywords here:
CSSearchableItemAttributeSet *contentAttributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
contentAttributeSet.title = activity.title;
contentAttributeSet.displayName = activity.title;
contentAttributeSet.keywords = [activity.keywords allObjects];
contentAttributeSet.contentDescription = @“My Description Here";
contentAttributeSet.thumbnailData = [self generateImage];

activity.contentAttributeSet = contentAttributeSet;
like image 431
Jordan H Avatar asked Dec 24 '15 21:12

Jordan H


2 Answers

If the title property for both NSUserActivity and CSSearchableItemAttributeSet is specified, then

  1. Item is searchable through both the titles.
  2. The search result will have the title specified in NSUserActivity instance.

If the keyword property for both NSUserActivity and CSSearchableItemAttributeSet is specified, then the item is searchable using the keywords specified in the CSSearchableItemAttributeSet instance and not the keywords specified in the NSUserActivity instance.

There are no conflicts when properties are set either for NSUserActivity or CSSearchableItemAttributeSet.

So, when using CSSearchableItemAttributeSet with NSUserActivity, we can skip setting the CSSearchableItemAttributeSet properties which are common to NSUserActivity class.

like image 98
PGDev Avatar answered Oct 19 '22 22:10

PGDev


After talking with DTS on this topic, this is their conclusion:

With regards properties, like keywords, that can be set on both the NSUserActivity and the NSUserActivity’s embedded CSSearchableItemAttributeSet, the advice from Core Spotlight engineering is that you set them just on the CSSearchableItemAttributeSet.

[title and displayName] are more-or-less the same, with the soft implication that, if the item has a really long title, that'd go in the title property and the abbreviated title would go in the displayName property.

like image 34
Jordan H Avatar answered Oct 20 '22 00:10

Jordan H