How to add Core Spotlight to index content in your app


Before getting into the above topic lets have a short preview about what is a spotlight search for those who are new to iOS.

Spotlight Search : Spotlight search is an iOS feature through which you can search for anything in your iPhone/iPad (apps, music, videos, contacts and even information stored inside apps). The following screenshot will let show how a spotlight search looks like in a iPhone device.

To access spotlight you have to perform a swipe down action in your home screen.

Core Spotlight Index_1_of_2

Now lets get into our main goal of this blog.

•From iOS 9  Apple has added many new features one such feature gives us a way to display content of our apps through spotlight search which are called search APIs.

•In this blog let us see the key components involved in the search API and make our app content display as a result of spotlight search.

•The two main key component involved in search APIs are,


Core Spotlight Framework.

Short Notes:

◦ NSUserActivity: Helps to capture and restore app state through a process called Handoff(Lets users to begin an action in one device and continue the action on another device).

◦ Core Spotlight Framework: Helps to add, edit and remove items from the on-device index for Spotlight search.

Steps to use Core spotlight Framework to index our app content:

To use this framework to index our app content perform the following steps,

◦ First of all add the Core Spotlight framework into the project. To add the core spotlight framework Goto,

Project Navigatior -> Select the Project -> Build Phases tab- > Link Binary With Libraries

And press the “+” button, a search box opens search with keyword “corespotlight.framework” and also add another library called “MobileCoreServices.framework”.

Then the following steps has to be added to make your app content visible in the spotlight search result.

1.) First create an CSSearchableItemAttributeSet object with following details,

-> Its ”title”,  “contentDescription” & “thumbnailData” as your wish.

2.) Create  an CSSearchableItem object with,

“uniqueIdentifier” : Any unique string value which you would like to assign for your search item.

“domainIdentifier” : A domain string value as you like &

“attributeSet” : set of keywords using which you would like to search the search item created above.

3.) Use “CSSearchableIndex.defaultSearchableIndex(). indexSearchableItems ([CSSearchableItem])” method to add the above created search object into the index.

The following sample code helps you understand it better,

//Adding app content into on-device index.
func createSearchableItemAndAddToIndex() {
      // First Create an attributeSet object
      let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
      let titleString = “Title goes here”
      let titleDesctiption = “Description goes here”
// Add details into attributeSet
attributeSet.title = titleString
attributeSet.contentDescription = titleDesctiption
attributeSet.thumbnailData = UIImageJPEGRepresentation(UIImage(named: “profileImage.jpg”)!, 0.5)
// Create keywords based on which we can able to search in the spotlight
var searchKeywords = titleString.componentsSeparatedByString(” “)
        attributeSet.keywords = searchKeywords
       // Create a CSSearchableItem with a uniqueIdentifier, domainIdentifier & attributeSet
       let item = CSSearchableItem(uniqueIdentifier: “uniqueID”, domainIdentifier: “identifier”, attributeSet: attributeSet)

      // Add the item to the on-device index.

CSSearchableIndex.defaultSearchableIndex(). indexSearchableItems([item]) { error in
      if error != nil {
      else {
            print(“Search item has successfully added to Core Index”)

After executing the above code you can see the above search item in the spotlight search result as shown in the following screenshot.

Now we have added our app content into on-device index and displayed it in the spotlight search result. Now on the other hand, by default when the user clicks on the search result it will take the user to the respective app.

Core Spotlight Index_2_of_2

To make the app do our custom action like opening a specific screen in the app when user clicks on the search result we have to handle it. This can we done with the help of following steps.

Using NSUserActivity we can achieve this. So learn about NSUserActivity before proceeding further. I have given a sample overview of how this could be achieved.

//Custom action for search result items

Whenever a user clicks the search result item the following delegate method will get called in AppDelegate.

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool;

If you would like to make custom action for your search result selection action you can make use of this method and perform your required action by calling the method “restoreUserActivityState” from the above delegate method.

An example code for handling custom user action is shown below,


func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
if userActivity.activityType == CSSearchableItemActionType {
let uniqueIdentifier = userActivity.userInfo? [CSSearchableItemActivityIdentifier] as? String
let splitController = self.window?.rootViewController as! UISplitViewController
let navigationController = splitController.viewControllers.first as! UINavigationController
navigationController.topViewController?. restoreUserActivityState(userActivity)
return true
Next you have to override the “restoreUserActivityState” method in your view controller and from there you can perform your required actions like showing the required screen or any actions as you would like to perform.
An example for overriding the restoreUserActivityState is shown below,
override func restoreUserActivityState(activity: NSUserActivity) {
if let contactNumber = activity.userInfo![CSSearchableItemActivityIdentifier]! as? String {
// Perform your custom action here or make the user to move to other screen in your application
self.performSegueWithIdentifier(“viewController”, sender: self)
I am an iOS developer and the this feature helps me to expose my app contents in spotlight search, which makes it easy in accessing the contents from my apps without even navigating many pages into my app, to get to that particular detail.

Until Next Time……

iOS Developer,
Mallow Technologies.

Leave a Reply

%d bloggers like this: