I am trying to index the NSUserActivity index using a private index in Spotlight on iOS. I followed all the steps in the Apple Index Activities and Navigation Points guide , but my activity does not seem to be indexed at all by the searchlight.
The manual says that:
To ensure that an activity and its metadata are indexed, you must keep a strong link to the activity until it is added to the index. There are two ways to do this: the first way is to assign activity to the controller object that creates the Events. The second way is to use the userActivity property of the UIResponder Object.
I decided to make the first option (to create a property in my view controller to store NSUserActivity).
var lastSearchedUserActivity: NSUserActivity?
The idea is that when a user searches for something, his last request is indexed on the device. I have the following method that prepares user activity and (presumably) 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 is called without a problem, but the action is not searchable: I tried to search Spotlight using its assigned titleand keywords. Activity never appears.
I have tried many solutions, including:
To move the call eligibleForSearchimmediately after the activity is created. Apple's management says nothing about this, but the code snippets in the link provided seem to imply that setting this line to trueshould automatically add activity to the index.
Apple , becomeCurrent(), , (? ). , , . . . Apple , becomeCurrent() eligibleForSearch true .
view controller userActivity . , .
, , Apple . .
iPhone 6S +, Spotlight. , Spotlight.
:
self userActivityWillSave.
NSUserActivityDelegate , userActivityWillSave:
, .
, , . :
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")
}