Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make App Activities and States Searchable by using NSUserActivity

Following is the code that I am trying to implement to make app activities and states searchable but not able to show on iOS search

NSUserActivity *userActivity = [[NSUserActivity alloc]initWithActivityType:@"com.mycompany.activity-type"];

userActivity.title = @"Hello world from in app search";
userActivity.keywords = [NSSet setWithArray:@[@"Hello",@"Welcome", @"search"]];
userActivity.userInfo = @{@"id":@"com.example.state"};

userActivity.eligibleForSearch = YES;    
[userActivity becomeCurrent];

Link to make my question more clear.

like image 218
Ayush Avatar asked Jun 15 '15 01:06

Ayush


2 Answers

From the Apple Forums:

One thing that has bitten a few people (myself included) is that the activity must not be deallocated. If your code is only working with NSUserActivities (i.e. not using CoreSpotlight in addition) then make sure your activities aren't being deallocated immediately.
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.

https://forums.developer.apple.com/message/13640#13640

like image 104
L A Avatar answered Oct 31 '22 15:10

L A


What I have found is you have to assign the NSUserActivity instance you have created to your currently visible UIViewControllers's userActivity property before calling -becomeCurrent. It has fixed it for me and the items immediately appeared both for handoff on other devices and in spotlight search on the same device.

like image 31
Zoltán Lippai Avatar answered Oct 31 '22 14:10

Zoltán Lippai