06 October

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.

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:

  • Create an object: CSSearchableItemAttributeSet
  • 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)
  • Create an object: CSSearchableItem with a unique identifier, domain and previously created object CSSearchableItemAttributeSet
  • 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.