To find something, you can swipe down from the top and enter the content. It enables us to quickly find the application. But the result set contains only the name of the iPhone applications matching the content written in the search field. But if I tell you that you can search within the app without actually opening content and data, all at one place. Yes, it’s possible now using Apple’s Core Spotlight Framework.
Core Spotlight Framework
Core Spotlight is a framework that enables us to see the inside content of the iPhone application without actually opening it and finding it in the application. It gives the ability to increase the visibility as well as discoverability of the application’s content significantly. Now we don’t have to open the application and then search what we are looking for in it.
The Core Spotlight framework helps the app participate in a search by providing ways to index the content within the iPhone application (including user-generated content) and manage the on-device index. Use the APIs of the Core Spotlight framework to add, edit, and remove items from the on-device index.
Making App Content Searchable
To index the app content, one has to perform the below mentioned steps:
- Create a CSSearchableItemAttributeSet object and specify properties that describe the item that needs to be indexed.
- Create a CSSearchableItem object to represent the item. A CSSearchableItem object has a single identifier that lets you refer to it later.
- If needed, specify a domain identifier so that you can gather multiple items together and manage them as a group.
- Associate the attribute set with the searchable item.
- Add the searchable item to the index.
- In order to index a piece of data, we must create an instance of CSSearchableItemAttributeSet. The object of CSSearchableItemAttributesSet encapsulates the set of properties that will be shown for a searchable item. In other words, it contains the metadata that will be displayed when the item will be a list in the search result.
Set a uniform type identifier of the content let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
Set the title of the item searchableItemAttributeSet.title = “Spotlight Indexing”
Pass URL of the thumbnail image for the item searchableItemAttributeSet.thumbnailURL = imageUrl
Set the description of the item’s content. searchableItemAttributeSet.contentDescription = “We are seeing for this content directly in spotlight search”
An array of keywords associated with the itemsearchableItemAttributeSet.keywords = keywords
- Once we store the attributes, we need to assign it to a CSSearchableItem, which is an object that is passed for Spotlight Indexing.
The specified identifier, the domain identifier and attribute set associated with the attribute, returns the correct item.
let searchableItem = CSSearchableItem(uniqueIdentifier: “identifier”, domainIdentifier: nil, attributeSet: searchableItemAttributeSet)
- uniqueIdentifier uniquely identifies the searchable item on the Spotlight.
- domainIdentifier is used to group together multiple searchable items.
- attributeSet is the object on what we just assigned values.
Now all we need to do is get a reference to the Spotlight index with a call to CSSearchableIndex.defaultSearchableIndex() and call indexSearchableItems:completionHandler: with the searchable items that we have created and we’re done with the indexing.
To make multiple objects searchable, create an array of CSSearchableItem and pass it in indexSearchableItems:completionHandler:. All of the array’s items will be indexed.
We are ready to search for the items that we indexed using the above code. If you search for Avatar in the search, the search result will now contain the Avatar titled card along with its image and description. We can also search using any of the supplied keywords.
Extended Search Item Attributes
In the above examples, we just created a very simple type of item containing just a fewer amount of information that needs to be indexed. However, there is a multitude of other types of data that can include in an attributeSet. Some of the categories of attribute set class are:
- Documents allows the storage of content related to text documents like page count, page height, font, etc
- Events allows the storage of data related to events like start date, end date, due date, etc
- Places allows the indexing of data related to location like latitude, longitude, city, country, distance, speed, etc
- Media enables us to store details of media like copyright, codecs, duration, streamable, genre, rating, etc
- Images makes it possible to store information like resolution, exposure, pixel count, orientation, pixel width, pixel height, etc
- Messages allows us to store email addresses, phone numbers, recipient addresses, recipient names, etc
Using the Core Spotlight Framework, Apple has increased greatly on the way to search on the iPhone. It gives us an opportunity to increase the iPhone application’s content visibility and discoverability, also increasing the ease of access to the app’s content all at one place. Implementation of such features does not increase user experience and retention but iPhone App Development India makes it more accessible to users.