App data indexed in spotlight search

With iOS 9, Apple bring many new things for iOS developer. Let’s talk about spotlight search. In iOS 9 new frame work added called “Core Spotlight” which is used to add our app information searchable in spotlight search.

There are 2 way to indexes:

  • A private on-device index
    Each device contains a private index whose information is never shared with Apple or synced between devices. When you make an item available in a user’s on-device index, only that user can view the item in search results.
  • Apple’s server-side index
    The server-side index stores only publicly available data that you’ve marked appropriately on your website.

Now, we are going to see the first way of index by example.

Let’s create new project which display list of restaurants in tableview.

First we create plist file to store information of restaurant [name, description & image].

snap1

After that we will fetch restaurant list from plist file & store in array of restaurant model class.

Now as iOS developer we know tableview control, so we will display list of restaurants in table

Using below code we are able to display restaurant’s list in tableview.

snap2

Before we start integration of spotlight search, we must import two frameworks first:

Here we have to understand 3 class & use of it:

  • CSSearchableItem
    First we will create searchable item object & configure with attribute set & domainIdentifier.
    DomainIdentifier is used to identify the items group. It’s option argument. We can delete all items of that identifier.
  • CSSearchableItemAttributeSet
    Using attributes we set basic properly like title, description, thumbnailURL & keywords.
    title: Heading title of our item in search result.
    contentDescription: 2 lines of description in search result below the title.
    thumbnailURL: file url which is stored in app
    thumbnailData: you can store server image. First download from url set as Data.
    keywords: Indicate that user searches by keyword. If items match, it will list in spotlight.
  • CSSearchableIndex
    Finally we add our items in array object using indexSearchableItems method of CSSearchableIndex class.

See the code snippet.

let go line by line to make clear.

  • First, Create searchableItem array Object which hold all items to indexed in spotlight search.
  • second, Create searchableItemAttributeSet object & set all basic property.
  • Without keyword there is no result in spotlight. We must set related keyword to item.
  • Then configure the item with identifier & attributeSet append to searchableItems array object.

At last by calling indexSearchableItems it’s indexed to spotlight search. Here we have to pass items array.

snap3

snap4

It’s not finished yet buddy, Still we have one more interesting thing to learn.

How to handle item tap event in spotlight & navigate to specific screen in our app.

In our appDelegate file we have delegate method which received event when user tap on item.

We have implemented this method & handle ahead using NSNotificationCenter and post a custom notification, which handle in the ViewController class.

userActivity object contain userInfo dictionary has identifier value which we set during created CSSearchableItem object in addToSpotlight method.

For more handling code download our project & run it.

snap5

We missed one more thing, after adding at spotlight if we need to update or delete item, How we can do that?

We have to look in CSSearchableIndex class have method for update & delete items. We will look only delete here.

see the below code snippet for delete.

It will remove all the items from the spotlight index.

mm

Chirag Daslaniya Sr. iOS Developer

Chirag Daslaniya is a passionate Mobile Application Developer, especially for iOS at Yudiz Solutions Pvt. Ltd. – a leading iPhone App Development company. He is zealous about making easy-to-use and user-friendly applications using simple yet striking interfaces.

Leave a Reply

Top