JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

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:

  1. Create a CSSearchableItemAttributeSet object and specify properties that describe the item that needs to be indexed.
  2. Create a CSSearchableItem object to represent the item. A CSSearchableItem object has a single identifier that lets you refer to it later.
  3. If needed, specify a domain identifier so that you can gather multiple items together and manage them as a group.
  4. Associate the attribute set with the searchable item.
  5. 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.

    Example:

    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 item

    searchableItemAttributeSet.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.

    Example:

    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)

    Params:

    1. uniqueIdentifier uniquely identifies the searchable item on the Spotlight.
    2. domainIdentifier is used to group together multiple searchable items.
    3. 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.

Code:

let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) searchableItemAttributeSet.title = “Avatar” searchableItemAttributeSet.thumbnailURL = “http://t0.gstatic.com/images?q=tbn:ANd9GcQCfmvrE4fMo2cd8esc7mDZPtFSJThAujddMPkRtti1_ij6u-jp” searchableItemAttributeSet.contentDescription = "A transparent marine sent to Pandora on a unique purpose is torn apart by serving its order and guarding the world, which it feels is its home.” searchableItemAttributeSet.keywords = [“Movie”, “Film”, “James Cameron”, “Pandora”, “2009”] let searchableItem = CSSearchableItem(uniqueIdentifier: "identifier", domainIdentifier: "movies", attributeSet: searchableItemAttributeSet) CSSearchableIndex.default().indexSearchableItems([searchableItem]) { (error) -> Void in if let error = error { print(error) } else { print(“Content Indexed Successfully”) } }

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.

Example:

var searchableItems = [CSSearchableItem]() for i in 0...(movies.count - 1) { ... let searchableItem = CSSearchableItem(uniqueIdentifier:”identifier\(i)", domainIdentifier: "movies", attributeSet: searchableItemAttributeSet) searchableItems.append(searchableItem) } CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) -> Void in if let error = error { print(error) } else { print(“Content Indexed Successfully”) } }

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:

  1. Documents allows the storage of content related to text documents like page count, page height, font, etc
  2. Events allows the storage of data related to events like start date, end date, due date, etc
  3. Places allows the indexing of data related to location like latitude, longitude, city, country, distance, speed, etc
  4. Media enables us to store details of media like copyright, codecs, duration, streamable, genre, rating, etc
  5. Images makes it possible to store information like resolution, exposure, pixel count, orientation, pixel width, pixel height, etc
  6. Messages allows us to store email addresses, phone numbers, recipient addresses, recipient names, etc

Conclusion:

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.

Image