Saturday 16 February 2019

Add Spotlight Search to Your App

Check source code at Github:
https://github.com/Sylvia-YiyinShen/SpotlightSearchDemo

Build and run
Then go to the Spotlight Search and type 'Blue', two search items in the demo app will appear and tapping on one of them takes to the app. A label in the center will indicate which item has been selected.




How to add Spotlight Search

  • import CoreSpotlight
  • Create a CSSearchableItem for any entry item you want to have in Spotlight. (Specifying title, thumbnail, description, id, domain....)
  • index items by invoke CSSearchableIndex.default().indexSearchableItems

Now you will be able to see the search items in spotlight.

Note: The already indexed items will not be deleted unless the app is uninstalled. If you want to remove old search items, explicitly invoke func deleteSearchableItems(withDomainIdentifiers domainIdentifiers: [String], completionHandler: ((Error?) -> Void)? = nil)


How to handle tapping event of each items

  • override AppDelegate function as below

  • override restoreUserActivityState in the rootViewController




No comments:

Post a Comment