Skip to main content

Making app data searchable with Core Spotlight framework



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

Popular posts from this blog

Lifecycle of React Native Component [2020 Edition]

What are the life cycle methods of React Native Component? A component's life cycle in React Native can be divided into 4 phases: React Native Component life cycle phases Mounting:  In this phase,  component instance is created and inserted into the DOM. Updating: In updating phase, a react component is said to be born and it start growing by receiving new updates. Unmounting: In this phase, a react component gets removed from actual DOM. Error Handling: It is called when any error occurs while rendering the component. Now let's discuss about different methods that gets called during these phases. Mounting phase Below are the methods which gets called when instance of component is created and inserted into the DOM. Constructor() static getDerivedStateFromProps() render() ComponentDidMount() Constructor() constructor ( props ) { super ( props ) ; this . state = { employeeId : 0 } ; } It is first method which gets called in the l

SOLID Design Principles in Swift

What are solid principles in programming world? 5 SOLID design principles in swift SOLID is acronym used in software programming for making software design more understandable, flexible, scalable and maintainable.  Every Software Developer must be aware of 5 SOLID principles in order to deliver good quality code SOLID stands for what? S -  Single Responsibility principle O - Open Closed Principle L - Liskov substitution Principle I - Interface segregation Principle D - Dependency Inversion Principle If we apply 5 SOLID principles while creating iOS/Mac Apps then the benefits which we will get are as follow: ·       We will have flexible code which can be changed easily. ·       Software code becomes more reusable. ·       Software developed will be robust, scalable and stable. ·       Code is loosely couple which means dependency between the elements is low. Now let’s discuss each principle one by one. 1.)  Single Responsibility Principl

What's new in iOS 13

Hi guys, Lets have a quick review on what new features Apple has released in its latest iOS 13 version. iOS 13 version iOS 13 makes old iPhone faster, last longer  - In iOS 13, Face ID unlocking will be 30% faster than before , app launch times are two times faster. Apple also found a way to make app downloads smaller, up to 60% on average. iOS 13 Dark Mode  - Apple introduced new dark mode option which changes the entire look of the operating system from light to dark. All native Apple apps feature Dark Mode support, and third-party apps can use Dark Mode APIs to add Dark Mode integration. iOS 13 features a QuickPath keyboard  - With iOS 13, Apple's default QuickType keyboard will be incorporating swipe-to-type, a popular way of sliding across the keyboard to form words. Before this, we have to use extensions like Google’s Gboard and SwiftKey Sign - In with Apple - Now, we can use Apple sign In to  conveniently sign in to third-party accounts.Sign-in with A