Getting Your iOS App Content Suggested in Spotlight Search

Prianka Liz Kariat
5 min readMar 15, 2017

--

Have you ever wondered how you can get content from your app suggested in spotlight search? Say, you have a location which you viewed in your app and you want to be able to search and find it again.

Proactive Suggestions are what you are looking at, if you want to achieve this.

App Overview

We are going to develop a simple app which shows a map that continuously updates the user’s location. At any point of time you can tag a location. Once tagged, if you do a spotlight search using tags or the title, the location will be brought up in the search. You can click on it and directly open the app.

One big advantage of this is that you can get your app suggested in search when user searches for some keyword. This can potentially increase user engagement. That is the outcome we all want from our released apps, right?

You can see a short demo of our application below.

In our app, you can enter hash tags associated with the place and give it a title. Now when you search by these keywords, your app will be shown in the search results. You can navigate to the location inside your app from search results.

This app has two view controllers:

TagLocationVC — The view controller responsible for handling tagging of a location.

ShowLocationVC — Shows the tagged location, when the search result is clicked by the user.

Let’s Start Coding!!!!

On clicking the tag button, the following function is called.

NSUserActivity helps us capture the state of our app and reinstantiate it. If you have done hand off, you would definitely have come across this handy class. To get your app suggested on spotlight search, NSUserActivity comes into picture.

var activityType: String { get }

This unique string identifies the activity. It is preferred that you give it in reverse DNS style( like the bundle identifier ).

You just have to set the following property of user activity to true.

var isEligibleForSearch: Bool { get set }

This adds the activity to the on device index.

Additionally if you set,

var isEligibleForPublicIndexing: Bool { get set }

This adds the activity to the public index which in turn lets it to be accessed by all iOS uses.

You can give a title to your activity. If you search by your title in spotlight search, you would get your activity in the results.

var userInfo: [AnyHashable : Any]? { get set }

userInfo will come in handy when you want to continue the user activity. You can specify any additional information you want to carry forward.

CSSearchableItemAttributeSet describes an item in search. You have to create a searchable item and associate it with the NSUserActivity.

itemContentType: Uniform Type Identifier for the item being added in search. Location does not have a reserved UTI, hence we can simply name it as “location”.

var thumbnailURL: URL? { get set }

This can point to any local file that holds an image you want to display along with the item, when it is displayed in the search results.

var supportsNavigation: NSNumber? { get set }

Useful if you want to directly open maps to navigate to the specified location. It will display the icon next to the item in the search results.

var namedLocation: String? { get set }

Specifies the name of the location. Again we set it to our title.

var latitude: NSNumber? { get set }

and

var longitude: NSNumber? { get set }

Specifies the actual location coordinates.

var keywords: [String]? { get set }

Keywords associated with the item in search. You can search by these keywords as well to get to your location. We are extracting the keywords from the hash tags entered by user in the first screen of our app.

func becomeCurrent()

This functions registers the activity with the system. In our case it adds the item to search.

Showing the Searched Location Within the App

As seen in the clip, when you click on the search result for the location, you can view the location in the app.

For this to happen, you have to add a key to Info.plist.

If you observe the code snippet, you can see that our activity types are of the order com.proactive.mapview.*

In AppDelegate the following delegate method needs to be implemented.

This delegate method continues the user activity. It is useful when you want to hand off an activity. We are regarding the opening of a search result as handing off an activity.

The activity is handed off to the root view controller. We drill down until we reach the respective view controller to display the location details.

In TagLocationVC we override the following:

func restoreUserActivityState(_ userActivity: NSUserActivity)

will be called on UIViewController when an activity is handed off to it.

var prevActivity: NSUserActivity?

Simply holds a reference to the activity being handed off.

Now we segue to ShowLocationVC which shows the location that was searched.

We call func restoreUserActivityState(_ userActivity: NSUserActivity)

on showLocationVC before segueing to ShowLocationVC so that the details in the activity is available to the next controller.

In ShowLocationVC,

var activity: NSUserActivity?

Holds the reference to the activity being restored. Now you simply add the code to populate the UI in viewWillAppear()

The code for adding an annotation on the map is self explanatory. Please feel free to go through MapKit tutorial to refer to the same.

So this sums up the steps required to add a location or an item to search.

Please feel free to go through the source code.

References

https://developer.apple.com/videos/play/wwdc2016/240/

--

--