I'm trying to get my NSUserActivity to be indexed by the private index in Spotlight in iOS. I have followed all the steps in Apple's Index Activities and Navigation Points guide but my activity doesn't seem to be getting indexed by spotlight at all.
The guide says that:
To guarantee that the activity and its metadata get indexed, you must hold a strong reference to the activity until it gets added to the index. There are two ways to do this: The first way is to assign the activity to a property in the controller object that creates the activity. The second way is to use the userActivity property of the UIResponder object.
I opted to do the first option (to create a a property in my view controller to hold the NSUserActivity).
var lastSearchedUserActivity: NSUserActivity?
The idea is that, when the user searches for something, his last query is indexed on the device. I have the following method that prepares the user activity and (supposedly) indexes it:
func prepareLastSearchedUserActivity(tags: [String], server: Server) {
if Settings.applicationIndexedUserActivitiesAsShortcutTypes.contains(.LastSearched) {
print("Get ready to index. and tags \(tags.reduce("") { "\($0) \($1)" })")
let activity = NSUserActivity(activityType: ShortcutType.LastSearched.rawValue)
activity.title = server.serverName
activity.userInfo = ["tags": tags, "server name": server.serverName]
let attributeSet = CSSearchableItemAttributeSet()
attributeSet.contentDescription = tags.reduce("") { "\($0) \($1)" }
attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue
activity.contentAttributeSet = attributeSet
activity.keywords = Set(tags)
activity.eligibleForSearch = true
activity.eligibleForHandoff = false
activity.becomeCurrent()
self.lastSearchedUserActivity = activity
//self.lastSearchedUserActivity?.becomeCurrent()
}
}
This method gets called without an issue, but the activity is not searchable: I have tried to search in Spotlight using the title
assigned to it, and the keywords
. The activity never shows up.
I have tried many solutions, including:
To move the
eligibleForSearch
call directly after creating the activity. Apple's guide doesn't say anything about this directly, but the code snippets in the provided link seem to imply that setting this line totrue
should add the activity to the index automatically.Apple doesn't say that
becomeCurrent()
should be called, but rather that it will be called for you (how? No idea). Regardless like you can see, I tried calling it myself. I also tried calling it after assigning it to my property. No dice. Apple does say that when callingbecomeCurrent()
on an activity that haseligibleForSearch
astrue
, it will be added to the index.I even went as far as using the view controller's
userActivity
property directly for the creation and configuration of the activity. Because I'm using the provided property, it shouldn't be deallocating early.
As far as I can tell, I am doing everything Apple does in their guide. I am completely lost.
I am testing on an iPhone 6S+ so Spotlight indexing is available. The console does not print anything related to Spotlight, either.
Update:
I just set the delegate of the activity to self
and implemented the userActivityWillSave
method.
According to the NSUserActivityDelegate
documentation, about userActivityWillSave:
Notifies the delegate that the user activity will be saved to be continued or persisted.
So this delegate method is getting called, but the indexed item is nowhere to be found. Here's the updated code:
func prepareLastSearchedUserActivity(tags: [String], server: Server) {
if Settings.applicationIndexedUserActivitiesAsShortcutTypes.contains(.LastSearched) {
print("Get ready to index. and tags \(tags.reduce("") { "\($0) \($1)" })")
let activity = NSUserActivity(activityType: ShortcutType.LastSearched.rawValue)
activity.title = server.serverName
activity.userInfo = ["tags": tags, "server name": server.serverName]
activity.delegate = self
let attributeSet = CSSearchableItemAttributeSet()
attributeSet.contentDescription = tags.reduce("") { "\($0) \($1)" }
attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue
activity.contentAttributeSet = attributeSet
activity.keywords = Set(tags)
activity.eligibleForSearch = true
activity.eligibleForHandoff = false
self.lastSearchedUserActivity = activity
activity.becomeCurrent()
//self.lastSearchedUserActivity?.becomeCurrent()
}
}
func userActivityWillSave(userActivity: NSUserActivity) {
print("Yep it will save")
}