Spotlight Search for Your Indexed Swift App Records

Mazen Kilani
Geek Culture
Published in
4 min readMay 13, 2021

Spotlight Search Allows the User to Find Your App Records from the iPhone / iPad Native Search Bar

Spotlight Search Finding a Book in the 8Bookss App

Scenario

Our app will allow users to add, or remove, specific record fields to, or from, the native iPhone / iPad Spotlight search. In our example of the Books app, the Book Title will be our Spotlight search term. The user can choose to add the title to Spotlight in the New Book or Edit Book views, and, optionally, remove the title from Spotlight in the Edit Book view.

Technique

Swift uses specific Spotlight functions and methods to index (add) or remove (delete) the searchable items (e.g., Book Titles). The searchable item value is referred to as an ‘identifier’, your domain as ‘domainIdentifier’, and your app name, usually, as ‘contentDescription’.

Index Functions:

CSSearchableItem(..), CSSearchableIndex.default().indexSearchableItems(..)

Delete Functions:

CSSearchableIndex.default().deleteSearchableItems(..)

Audience

The article is for Swift developers who seek complete, proven, code-centric solutions to speed up their development projects. The code snippets below can be used with minimal customisation.

App Model

We based the article on the AppsGym Books model app, published on Apple’s App Store (as 8Books), and you can download the complete Xcode project on AppsGym.com, for free.

User Interfaces

We utilise a Yes/No dialogue, using button outlets and actions, to index or delete the Spotlight search item. The button colour changes to either Green (Yes) or Red (No) when user taps the relevant button. We also use an image to indicate the status of the Spotlight search; a checkmark indicates indexed and a red circle indicates not indexed. You may choose to use a Switch instead in your app and different images, of course.

NewBookTableViewController.swift displays 2 grey buttons for Yes and No. If the user taps Yes then the Book Title is indexed (added) to Spotlight when the book record is saved. Otherwise, no action is performed.

New Book Add to Spotlight Dialogue

EditBookTableViewController.swift displays the current status of the Spotlight search (a checkmark indicates title already indexed) and the 2 grey buttons of Yes and No. If the user taps No then the Book Title is removed (deleted) from the Spotlight search. If the user taps Yes and the book title is not already in Spotlight then it is indexed (added) to Spotlight when the book record is saved. In your app, you may choose to reflect the status, button colours, and possible actions differently.

Edit Book Shows Book Title Already Indexed
Edit Book Shows Book Title Not Indexed

Logic

Firstly, we need to import 2 Swift libraries: CoreSpotlight and MobileCoreServices (the latter must be used with CoreSpotlight).

There are 2 main actions: Index (add) and Delete (remove), with the same parameter, the Book Title. So, we shall use 2 separate functions addToSpotlightSearch(_ title: String) and removeFromSpotlightSearch(_ title: String), and call each function depending on the status of the Yes and No buttons, respectively.

Connect both Yes and No buttons to the same @IBAction and turn On/Off the relevant flags (boolean variables) to indicate which button has been tapped. When you save the record, execute the Index (add/Yes) or Delete (remove/No) function depending on the flag values.

Code

Code: New Book

The Swift code snippets below are extracts from NewBookTableViewController.swift, which shows the Yes/No buttons outlets , button actions and the Spotlight function to add (the remove function is only used in the Edit Book). In our example Books app, we use a Core Data attribute isSpotlight to store the Spotlight status.

NewBookTableViewController.swift Variables & Buttons

NewBookTableViewController.swift viewDidLoad()

NewBookTableViewController.swift saveRecordToCoreData()

NewBookTableViewController.swift addToSpotlightSearch(..)

Code: Edit Book

The Swift code snippets below are extracts from EditBookTableViewController.swift, which shows the Yes/No buttons outlets, button actions and the Spotlight image as a status indicator. The Edit Book uses both functions to add and remove the Spotlight search term. In our example Books app, we use a Core Data attribute isSpotlight to store the Spotlight status.

EditBookTableViewController.swift Variables & Buttons

EditBookTableViewController.swift vewDidLoad()

EditBookTableViewController.swift saveRecordToCoreData()

EditBookTableViewController.swift Spotlight Functions

The article covered the complete setup, logic and code to add a new search term (book title) to Spotlight for a New Book, and to add a search term (book title) to, or remove from, Spotlight in an Edit Book view. In your app, you can refer to any of your entity record fields, and add your own domain and app name. Hope you find it useful. Thanks for reading!

--

--

Mazen Kilani
Geek Culture

I published 47 Swift iOS apps/games and 2 Flutter Android apps. I share complete Xcode projects, free (no ads), at AppsGym.com.