iOS 9 Core Spotlight search instance code
- 2020-06-19 11:45:40
- OfStack
preface
Spotlight is a useful feature that can increase user activity and increase exposure.
The body of the
1. Implementation (iOS 9.0)
1.1 Adding indexes
var searchableItems = [CSSearchableItem]()
for app in apps {
let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
searchableItemAttributeSet.title = app.label
searchableItemAttributeSet.contentDescription = "Watch \(app.label) LIVE on Shou"
let searchableItem = CSSearchableItem(uniqueIdentifier: app.id, domainIdentifier: "youappbundlerid.apps", attributeSet: searchableItemAttributeSet)
searchableItems.append(searchableItem)
}
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems, completionHandler: { (error: NSError?) in
if error == nil {
print("initSpotlight...completed")
} else {
print("\(error)")
}
})
Note that the above code added to the child thread execution, slightly slower. It is possible to request data from the server and index it in AppDelegate.
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 userActivity.activityType == CSSearchableItemActionType {
if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String, let label = userActivity.title {
// Page jump
}
}
}
return true
}
2. Extra experience
2.1 CSSearchableItemAttributeSet
You can leave thumbnailData unset, so that the default is LOGO as currently applied. Also note that thumbnailURL is not used to specify the location of web images - #, so you will need to download the web images locally before setting them
2.2 CSSearchableItem
The default failure is one month. In addition, it is found that more data cannot be carried (for example, setValue via CSSearchableItemAttributeSet cannot be received by userActivity).
2.3 The limit on the number of index bars was not found
http: / / stackoverflow com/questions / 35284223 / is - there - a limit - for - number - of - items - cssearchableitem - in - core spotlight -- cssear
I found that adding index is still very slow, and the other batch index did not understand, I think it should be deleted and added if suitable for batch processing.
2.4 There is no way to find a reindex
Repeated calls are no problem, as long as the uniqueIdentifier match overwrites the old data
2.5 There is no way to improve the ranking
It seems that the system will have a set of algorithms to automatically improve the ranking.
Conclusion: The above is the example application of IOS Core Spotlight development and experience sharing, hoping to help the students who develop IOS.