Index App Content with Core Spotlight Tutorial

Spotlight is the system-wide search function of iOS. In iOS9 it is possible to index the content of an app inside Spotlight using the CoreSpotlight API. In this tutorial the text of a Text Field will be indexed inside Spotlight. This tutorial is made with Xcode 7 and built for iOS 9.

Open Xcode and create a new Single View Application. For product name, use IO9CoreSpotlightTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard. Drag a Text Field from the Object Library and place it at the top of the Main view. Drag two buttons from the Object Library and place them below the Text Field and next to each other. Change the titles of the button to "Add to Index" and "Remove from Index".

First add the Auto Layout Constraints. Ctrl and Drag from the Text Field to the top of the main View. Hold down the Ctrl key and select "Vertical Spacing to Top Layout Guide" and "Center Horizontally in Container"

Ctrl and drag from inside the Text Field to a bit to the right, while still inside the Text Field. Select the "Width" Constraint.

Ctrl and Drag from the Left button to the Text Field. Hold down the Ctrl key and select "Vertical Spacing" and "Leading".

Ctrl and Drag from the Right button to the Text Field. Hold down the Ctrl key and select "Vertical Spacing" and "Trailing".

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl and drag from the Text Field in the Storyboard to the ViewController class of the ViewController.swift file to create the following Outlet.

Ctrl and drag from the Left Button to the ViewController class  and create the following Action

Ctrl and drag from the Left Button to the ViewController class  and create the following Action

Go to the ViewController.swift file and import the following frameworks.

import CoreSpotlight
import MobileCoreServices

To dismiss the keyboard when pressing "enter" the ViewController needs to be setup as a UITextFieldDelegate. Change the class declaration to

class ViewController: UIViewController,UITextFieldDelegate {

In the viewDidLoad method  add the following line.

textField.delegate = self

Implement the textFieldShouldReturn(textField) delegate method.

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return false
 }

Add a constant property to set the Identifier of the item that needs to be indexed in Core  Spotlight.

let identifier = "MyIdentifier"

Implement the AddItemToCoreSpotLight(sender) Action method

@IBAction func AddItemToCoreSpotlight(sender: AnyObject) {
        let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
        attributeSet.title = textField.text
        attributeSet.contentDescription = "CoreSpotLight tutorial"
        
        let item = CSSearchableItem(uniqueIdentifier: identifier, domainIdentifier: "com.ioscreator", attributeSet: attributeSet)
        CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { (error: NSError?) -> Void in
            if let error =  error {
                print("Indexing error: \(error.localizedDescription)")
            } else {
                print("Search item successfully indexed")
            }
        }
    }

The CSSearchableItemAttributeSet object sets the properties and type that needs to be indexed. The title of the attributeSet is equal to the text entered inside the Text Field. The CSSearchableItem defines the item that can be indexed in Spotlight. Next, implement the RemoveItemFromSpotlight(sender) Action method

@IBAction func RemoveItemFromCoreSpotlight(sender: AnyObject) {
        CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithIdentifiers([identifier])
            { (error: NSError?) -> Void in
                if let error = error {
                    print("Remove error: \(error.localizedDescription)")
                } else {
                    print("Search item successfully removed")
                }
        }
    }

The previously created item with our Identifier will be removed from Spotlight. Build and Run the project. Enter a title in the Text Field and click "Add to Index"

Press Shift+Cmd+H to go to the home screen and slide left to go to the Search field of Spotlight.Search for the entered title and the indexed item should be visible. Clicking this item will bring you back inside the app.

You can download the source code of the IO9CoreSpotlightTutorial at the ioscreator repository on Github.