Introduction
In this article, we will explore
Core Spotlight framework. We are all aware with the functionality of Spotlight
search in iOS devices. It can search any content present in our iPhone
including your movie collection, music, contacts, email. It also performs
launching apps installed in device.
Could we search our app data with spotlight search ?
Well, the answer is yes. We can
make our app data searchable on spotlight search using Core Spotlight
framework. It is introduced by Apple in iOS 9 which gives opportunity to
programmers to increase the discoverability, visibility and ease of access of
their apps significantly, and in a fashion that wasn’t possible to be used in
earlier versions of iOS.
The Core
Spotlight Framework makes the data of an app searchable on the
Spotlight, and subsequently bring results regarding an app along with any other
results that the system returns. Developers are also given the power to drive
users to specific view controllers that are most appropriate and suitable for
the data that were selected on Spotlight.
Get Started
Integration of core spotlight framework in iOS apps
doesn’t consist of a complicated process. In order to make app data searchable
we need to add our data to iPhone’s local search index for discovery by the
user.
Now, Let us see its implementation.
I have created application which populates list of
movies along with information about them such as its actors, director, summary,
rating etc. On tapping any row, detailed description of movie will be shown on another view controller.
All
movie data is being fetched from plist file. You can see structure of plist
from below screenshot.
Let have a look how we are
displaying above information from below image.
Indexing
data for spotlight
The key task for making our app data searchable is to
ask specifically from the Core Spotlight API to index our data so it can be
found on searches performed by the user.
We must represent our app data as CSSearchableItem
objects which 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 makes the details of the searchable item, like what pieces of data should
be displayed upon searching (for example, the name of the movie, its image and
the description), and what are the keywords that make our app data to 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.
To
get started indexing app content, we need to perform below steps.
1.
Set the attributes for each single piece of data, for example a movie (CSSearchableItemAttributeSet
object).
2.
Initialize
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.
Now it’s time
to index our data.
Open
ViewController and import two frameworks mentioned below: 1.) MobileCoreServices/MobileCoreServices
2.) CoreSpotlight/CoreSpotlight
Now, create setupSearchableContent method in
ViewController file in which we’ll create a CSSearchableItemAttributeSet
object, and then we’ll set its attributes. We’ll specify the movie title,
description and image as the pieces of data that will be displayed to the user.
We also need to add keywords which is most crucial for the discoverability of
your app from the Spotlight Search. Suppose we want to make our app data
searchable on the basis of movie names, then we have to set all movie names as
keywords.
Let us now see the code to achieve required indexing.
In above code , we created CSSearchableItemAttributeSet
object and then we set the attributes for the data that we want show in search
results. Remember , we must add keywords to make our app data searchable , If
we forgot to add keywords then there will no results shown on the Spotlight for the app. Then we
initialise the CSSearchableItem object with unique identifier and
CSSearchableItemAttributeSet.
CSSearchableItem takes below parameters.
•
uniqueIdentifier: This parameter
uniquely identifies the current searchable item on the Spotlight. In this example we append the index of the current
movie to the identifier, because we’ll need it later to display the details of
the movie matching to that index. You’ll better understand the usefulness of
the movie index in a while.
•
domainIdentifier: Use this parameter to group
together searchable items.
•
attributeSet: It’s the attributes set object that we
assigned values to just right before.
At last we added CSSearchableItem object in
searchableItems array. Finally we used core Spotlight API to index our items.
Now call above newly created function right after getting details from plist.
Run the app, exit from it and then use the Spotlight
using any of the keywords we set above. You’ll will be amazed to see your app
contents appearing in Spotlight Search. By tapping on any result, the app will
be automatically launched.
Well, it looks quite impressive, however we can do
better than that. Now our goal is to make it
possible to present directly the detail view controller and see the
information for the selected movie. To achieve this, we need to override a
UIKit function named restoreUserActivityState and handle the selected result on
the Spotlight.
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 movie in the
moviesInfo array, and we’ll present the detail view controller.
Let us see implementation now:
We need to write above function in our ViewController.
String “identifier” is the unique identifier of the selected search result
which we have defined in CSSearchableItem object. We break the identifier in two parts and access the last object
which is index of the selected movie in the
movies collection.Then we get the required movie from our movieInfo array by
using above index and pass it to DetailView Controller. Rest of the code
will be handled by our existing implementation.
Now switch to AppDelegate. Here we have to implement one
delegate function which will be called
every time a result regarding our app gets tapped on the Spotlight, and its our responsibility to call the function we
implemented right above, passing of course the user activity object.
Let’s see the implementation:
Now run the app and search movie name in the Spotlight. On tapping any row, detailed description of movie will be shown. Implementing above functionality will definitely make our apps more discoverable and easy accessible to user.
If you enjoyed reading this article🙂, then please don't forget to share it with your friends and do subscribe this blog to receive more technical posts in future via email.
Comments
Post a Comment