paint-brush
Spotlight Search in iOSby@pleelaprasad
472 reads
472 reads

Spotlight Search in iOS

by Leela Prasad PENUMUTCHUAugust 14th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.
featured image - Spotlight Search in iOS
Leela Prasad PENUMUTCHU HackerNoon profile picture

  • Why it is helpful?

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.

  • OverView(Apple) :-

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:

  • For this we ask the CoreSpotlight API to index our data, so it can be found on searches performed by the user. 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 form. All that means, all the data that we want to make searchable through the Spotlight must be represented as CSSearchableItem objects, and then to be grouped together as an array and to be given to the CS API for indexing.
  • A single CSSearchableItem object contains a set of attributes that make crystal clear to iOS the details of the searchable item, like what pieces of data should be displayed upon searching (for example, the name of the brand, and its image), and the keywords that make data from our app appear on the Spotlight search.
  • All the attributes for a single searchable item are represented by a CSSearchableItemAttributeSet object that provides many properties for assigning the values we need.
  • Indexing the data for the Spotlight is the last action that should be always done.

The usual and normal flow involves the following steps (including indexing):

  1. Set the attributes for each single piece of data, for example a car object (CSSearchableItemAttributeSet object).
  2. Initialise a searchable item for each piece of data, using the attributes from the previous step (CSSearchableItem object).
  3. Collect all searchable items into an array.
  4. Index the data for the Spotlight using the above array.

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,

  • Define an array to hold the individual CSSearchableItem objects
  • loop through all the data items and then
  • initialise a CSSearchableItemAttributeSet with contentType
  • set the attributes for this CSSearchableItemAttributeSet object for each individual item from the loop(like title, thumbnailURL, description, etc)
  • Set the Keywords to this CSSearchableItemAttributeSet object to identify our content from the spotlight search
  • Initialise a CSSearchableItem object with a unique identifier, domain identifier and the above CSSearchableItemAttributeSet object. This unique identifier will be used to catch the user selected item from the spotlight search. So suffix some info related to your data item at the end of this unique identifier. the domain identifier will be used to group the searchable items
  • Append the above defined CSSearchableItem object to the array you defined earlier in this method
  • finally, by using the Core Spotlight API, index the items by passing the array object that you get from the above.
  • Call this method in viewDidload.

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!****************************