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.
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.
◦ 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,
// Add the item to the on-device 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.
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,
Until Next Time……