By indexing our app content with the core spotlight framework APIs, we let the users search and access our app content through Spotlight search and Safari.
You can help users access activities and items within your app by making your content searchable. The Core Spotlight framework provides APIs to label and manage persistent user data like photos, contacts, and purchased items in the on-device index, and allows you to create links into your app.
This allows your app’s content discoverable, visible and easily accessible in the user’s device. In this tutorial we will complete this functionality, and at last you will realise how easy it is to make user’s life easy.
Let’s get started…
Now let’s say we have a list and on selection, it shows some details for the selected item(like the following gif). We need these items to show up in our spotlight results.
→ About the App, we are creating!
The demo app we are creating, consists a UITableViewController, which is embedded in a NavigationController. It shows a list of cars, with their brand name and image. on selection of the list item(Car), it navigates to a DetailVC screen, where we can see the same details of that particular car item in full screen. For this app’s purpose I created a plist file, to load our car items into the datasource model to feed our TableViewController class. Please have a look on the project files(download the sample project from the bottom of this tutorial).
First, we make our data is searchable from the spotlight. So when users search with the keywords of our content, the image and brand names will be displaying on the screen. We will define those keywords later as we go through.
Later, on tapping the searchedItem related to our app, our app will be launched and we show the detail screen related to that specific item.
→ In the demo app, I created a plist file with cars’ brand names and their image names. I added all the images into the assets folder, so we can populate our tableview with them. I created a model object, Car to make it easy to work. The project also contains a class named AssetExtractor to create url strings for the asset items(have a look into that class).
→ After finishing project setup, showing data in the MainViewController, and displaying DetailVC with relevant data upon selection of tableview cells, We start our actual part for this spotlight search functionality.
Indexing data to the spotlight framework:
The usual and normal flow involves the following steps (including indexing):
We follow all these steps in order, to accomplish the task.
In order to start, first we need to import two frameworks into the project file.
import CoreSpotlight
import MobileCoreServices
Here I write a little description what to do to complete this functionality and also include the code snippet images from the sample project. I put the comments to explain the code, so please have a look into the following images to get what to do.
To do!!!
-> write a method in your view controller, to setup the searchable content functionality
— > in that method,
For now, our app’s content is searchable from the spotlight search, and upon tapping or selecting the resulted item from our app, our app will be launched and the main view controller will be shown. To make more user friendly, We need to show the detail screen for that selected item.
This is very easy to accomplish. all we need to do is implement a method called, restoreUserActivityState(_ activity: NSUserActivity).
The parameter of the above function is a NSUserActivity object. That object has a dictionary property named userInfo, and that dictionary contains the identifier of the selected searchable item on the Spotlight. From the identifier we’ll extract the index of the item in the datasource array, and we’ll present the details view controller. That’s all.
To get that activity, we need to implement a function in our AppDelegate object. That is
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
}
This fun called whenever the user selected the item related to our app from the spotlight search. So here we call the above restore method and pass the userAcrtivity object.
That is all. we successfully accomplished our task. Congratulations!!!
You can download the sample project from here
— — — — — — — — — *********************** — — — — — — — — —
If you like my tutorials please follow me on medium, twitter & linkedIn accounts.
Thanks for reading…
****************************!See you!****************************