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 allows you to add content to spotlight search engine, so that, for example, an application used for watching movies allows for adding movies, actors, directors and reacting if users select an item, so that we can move them 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:

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

Content indexing

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 spotlight, there will be executed the AppDelegate method, that is:

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

where:

  • application is 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 a block that may be called when there is a need to continue 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 proper place.

Learn more

Introduction to CloudKit

As it turns out, many young developers have never created applications in the iOS Developer Center. Showing how to do it seems like a good idea, so I decided to prepare presentation about CloudKit and kill two birds with one stone.

Read 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

WebSockets on iOS – Real-time Communication That Doesn’t Slow Down the App

When you want to download data for the app, you probably use API RESTful interface. All it takes is to ask the server for the data and that’s it! This method works well when the app doesn’t need permanent access to new information. But what to do if the content has to be updated in real-time? A delay can make it impossible for the user to buy an item or make a sports bet, for example. But there’s a solution – WebSockets on iOS. Check out why you should implement them, and how to do it.

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 2017

Nomination in
M-COMMERCE category

Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORT & RECREATION category

17

client reviews

Clutch logo