Introduction to Core Spotlight Framework

14 / Jul / 2016 by Ashish Jain 0 comments

Core Spotlight APIs introduced in iOS 9 that allows your content searchable to users. Core Spotlight Framework has a design different than NSUserActivity that also allows apps content to be Searchable. It follows a database-style design and allows you to provide more information about a searchable content.

For more on NSUserActivity read Spotlight Search APIs and Introduction to NSUserActivity.

Indexing Data For The Spotlight

Now let’s say we have a list and on selection, it shows some details for the selected item. We need these items to show up in our spotlight results.

To make this happen we index the searchable items with Core Spotlight API. But neither our app nor the CS API decides what kind of data this is going to be. It’s our responsibility to prepare that data and provide it to the API in a specific format. Each searchable result is represented by a single CSSearchableItem object which contains all the attributes with details of the searchable item.

For your reference, please refer to official documentation link so that you check out all the supported properties.

Indexing the data for the Spotlight is the last action that should always be done. The usual and normal flow involve the following steps (including indexing):

Set the attributes for each CSSearchableItemAttributeSet object.
Initialise a searchable item for each CSSearchableItem object.
Collect all searchable items into an array.
Index the data for the Spotlight using the above array.

We need import two frameworks first:

import CoreSpotlight
import MobileCoreServices

Method to index the items for search is as follows.

func setupSearchableContent() {
var searchableItems = [CSSearchableItem]()
for i in 0...(moviesInfo.count - 1) {
let movie = moviesInfo[i] as! [String: String]
// ----- Set the attributes for each CSSearchableItemAttributeSet object.------
let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
// Set the title.
searchableItemAttributeSet.title = movie["Title"]!
// Set the movie image.
let imagePathParts = movie["Image"]!.componentsSeparatedByString(".")
searchableItemAttributeSet.thumbnailURL = NSBundle.mainBundle().URLForResource(imagePathParts[0], withExtension: imagePathParts[1])
// Set the description.
searchableItemAttributeSet.contentDescription = movie["Description"]!
var keywords = [String]()
let movieCategories = movie["Category"]!.componentsSeparatedByString(", ")
for movieCategory in movieCategories {
let stars = movie["Stars"]!.componentsSeparatedByString(", ")
for star in stars {
searchableItemAttributeSet.keywords = keywords
// ----- Initialise a searchable item for each CSSearchableItem object. -----
let searchableItem = CSSearchableItem(uniqueIdentifier: “com.ttnd.appName”, domainIdentifier: "movies", attributeSet: searchableItemAttributeSet)
// ----- Collect all searchable items into an array. -----
// ----- Index the data for the Spotlight using the above array -----
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) -> Void in
if error != nil {

Implementing Targeted Landing

For now, we made the item searchable. It won’t take us to details page just yet, it will only open the application. Now we need to make the action wherein the user selects the result.
For this, we need to implement two methods in our Application.


Implement the application(_:continueUserActivity:restorationHandler:) method in the AppDelegate class as follows.

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
let viewController = (window?.rootViewController as! UINavigationController).viewControllers[0] as! ViewController
return true

Now in viewController implement the restoreUserActivityState(_:) method as follows.

override func restoreUserActivityState(activity: NSUserActivity) {
if activity.activityType == CSSearchableItemActionType {
if let userInfo = activity.userInfo {
let selectedMovie = userInfo[CSSearchableItemActivityIdentifier] as! String
selectedMovieIndex = Int(selectedMovie.componentsSeparatedByString(".").last!)
performSegueWithIdentifier("idSegueShowMovieDetails", sender: self)

Check out the gif example below which shows the app “Programmers” with Search API Implemented and performing the search using keywords.


Leave a Reply

Your email address will not be published. Required fields are marked *