2
votes

I'm planning to make some of my app content publicly indexable, and for that I am using NSUserActivity. From my experiments so far, I've discovered that apparently the only activity that appears in the search results is the last one to get becomeCurrent called on. Is there a way to make all my activities searchable?

The following code is on my appDelegate:

for (Shop* shop in shopManager)
{
    NSUserActivity* activity = [[NSUserActivity alloc] initWithActivityType:ACTIVITY_OPEN_SHOP];
    activity.userInfo = @{@"additional1": shop.name};
    activity.eligibleForPublicIndexing = YES;
    activity.eligibleForSearch = YES;
    activity.keywords = shop.indexableKeywords;

    CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*)kUTTypeText];
    attributeSet.title = shop.name;
    attributeSet.contentDescription = shop.indexableDescription;
    attributeSet.keywords = [shop.indexableKeywords allObjects];

    [activity setContentAttributeSet:attributeSet];

    [activity becomeCurrent];
    [activities addObject:activity];
}
self.userActivities = [[NSSet alloc] initWithArray:activities];
3
Have you tried grouping them with a domain identifier? Have you watched the WWDC video?soulshined
@la_f0ka, did you happen to solve this? I can make my NSUserActivity appear if its not in a for loop. I also made a strong variable and stored my activities in it but still no luck.Ace Green
@la_f0ka Have you fixed the issue? I have also same requirement where multiple useractivities have to be registered in appdelegate method.V V

3 Answers

0
votes

Hey I have code that is very similar to yours and spotlight is able to index all of my NSUserActivity objects. My guess is that your NSUserActivity objects go out of reference as soon as the next iteration of the loop occurs. Try adding a strong property.

From this source on Apple Forums: https://forums.developer.apple.com/message/13640#13640

In my case, I had code that was allocating the NSUA, setting some properties on it, calling becomeCurrent, but then the object would go out of scope and deallocated. If you're doing this, try tossing the activity into a strong property to see if you can then see the results when you search.

Let me know if it still doesn't work.

0
votes

RequiredUserInfoKeys is the property of NSUserActivity that you have to set in order to work properly in search results.

activity.requiredUserInfoKeys = [NSSet setWithArray:@[@"additional1"]];
0
votes

I have met the same problem. I think the reason of this is that previous user activity has not enough time for indexing its metadata by system, the next user activity have became current user activity, so only last one is searchable.

My solution is put latter one into a dispatch_after block and delay 1.5 second, making each of them has time to be indexed.

If someone has a better solution, I would be grateful.