Searching Application Content in iOS in a Nutshell

Searching Application Content in iOS in a Nutshell

Today, I will talk about one of the frameworks added in iOS 9: Core Spotlight. API makes it possible to add content to the Spotlight search engine, so that, for example, an application used for watching movies allows for adding movies, actors, and directors. It also enables us to react if a user selects our item. Then, we can move him or her to the desired location within the application.

Core Spotlight – searching the content

For the purpose of this entry, I created a project in which I list employees of Holdapp. I will divide the whole task into two parts:

  1. Indexing our employees.
  2. Handling the event when the user selects an employee.

Content indexing with CSSearchableItemAttributeSet

To index employees you need to:

  1. Create an object: CSSearchableItemAttributeSet.
  2. Add to it attributes relevant to your case. It should also be noted that you can view not only text but also images, numbers, dates, and points on the map (all the available options are here).
  3. Create an object: CSSearchableItem with a unique identifier, domain, and previously created object CSSearchableItemAttributeSet.
  4. Add a list to CSSearchableIndex using CSSearchableItem.
// We created a list of employees that is filled with Employee objects
 var searchableItems = [CSSearchableItem]()
        for (i, employee) in employees.enumerate() {
//1.
            let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
//2.
            searchableItemAttributeSet.title = employee.name
            searchableItemAttributeSet.thumbnailURL = NSBundle.mainBundle().URLForResource(employee.imageName, withExtension: "png")

            var keywords = [String]()
            keywords.appendContentsOf(employee.appsMade)
            keywords.appendContentsOf(employee.favouriteLanguages)

            searchableItemAttributeSet.keywords = keywords
//3.
            let searchableItem = CSSearchableItem(uniqueIdentifier: "com.hldp.team.\(i)", domainIdentifier: "hldp_team", attributeSet: searchableItemAttributeSet)
            print(searchableItem)
            searchableItems.append(searchableItem)
        }
//4.
        CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) -> Void in
            if error != nil {
                print(error?.localizedDescription)
            }
        }

Event handling

When a user selects one of our items in potlight, there will be executed the AppDelegate method, that is:

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool

where:

  • application is the application object,
  • userActivity is an object that is used to respond adequately to what the user selected and which has no property: activityType, equal to uniqueIdentifier from CSSearchableItem object,
  • restorationHandler is a block that may be called when there is a need to continue the activity.

This function allows you to customize the application to user actions. If you handle the action, you return true. Otherwise, you return false.

To sum up, Core Spotlight is very clean and simple to implement. A few lines of code can highly increase the visibility of applications and make it easier for users to access the proper place.

Bartek

Bartek Byra

iOS Developer

Learn more

How to Send iOS Notifications in Different Languages? Guide to Dynamic Localization

The best way to set a connection between a user, provider, and an app? System notifications. They allow users to get the latest news in no time. This solution is easy to implement in apps dedicated to one market. But it gets complicated when the messages must be displayed in many languages. In such cases, the dynamic localization of remote notifications can be a real game-changer.

Read more

Promises on the Example of PromiseKit in Objective-C

Sooner or later every programmer encounters the problem of synchronous execution of certain actions. For example: get user information from the API, parse server response, save data to the database, update the view, and many others. To make it even more, at some of these stages you still need to deal with error handling. What should you do exactly?

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2021

Winning app in
EVERYDAY LIFE

Nagroda Legalnych Bukmacherów

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2023

Winning app in MCOMMERCE DEVELOPMENT

23

client reviews

Clutch logo