Preface
I feel that the Spotlight function is quite useful, which can increase user activity and increase the exposure of application content.
text
1. Implementation (iOS 9.0)
1.1 Add index
var searchableItems = [CSSearchableItem]() for app in apps { let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) = = "Watch \() LIVE on Shou" let searchableItem = CSSearchableItem(uniqueIdentifier: , domainIdentifier: "", attributeSet: searchableItemAttributeSet) (searchableItem) } ().indexSearchableItems(searchableItems, completionHandler: { (error: NSError?) in if error == nil { print("initSpotlight...completed") } else { print("\(error)") } })
Be sure to add the above code to the child thread to execute, which is slightly slower. You can request data from the server in AppDelegate and then add index.
1.2 Receive click events
@available(iOS 8.0, *) func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool { if #available(iOS 9.0, *) { if == CSSearchableItemActionType { if let uniqueIdentifier = ?[CSSearchableItemActivityIdentifier] as? String, let label = { // Page jump } } } return true }
2. Additional experience
2.1CSSearchableItemAttributeSet
You can not set thumbnailData, so that the default is the current application's LOGO; also note that thumbnailURL is not used to specify the network image address - - #, so if you want to add thumbnails, you need to download the online locally before setting it
2.2CSSearchableItem
The default invalid is one month, and it is also found that more data cannot be carried (for example, the setValue of CSSearchableItemAttributeSet cannot be received from userActivity)
2.3 No limit on index number found
/questions/35284223/is-there-a-limit-for-number-of-items-cssearchableitem-in-core-spotlight-cssear
I found that adding indexes is quite slow. In addition, I didn’t understand the batch index. I felt that it should be suitable for batch processing if it has been deleted and added.
2.4 It's useless to find a way to re-index
It's useless to call it repeatedly, as long as uniqueIdentifier matches, it will overwrite the old data.
2.5 No way to improve ranking was found
It seems that the system will have a set of algorithms to automatically improve rankings.
Summarize:The above is the IOS development Core Spotlight instance application and experience sharing. I hope it can help students who develop IOS.