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:
- Indexing our employees.
- Handling the event when the user selects an employee.
Content indexing with CSSearchableItemAttributeSet
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 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 touniqueIdentifier
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.