NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to search your app’s Spotlight index

Swift version: 5.2

Paul Hudson    @twostraws   

If you choose to index your app’s content using Spotlight (and you should), you can then use more Core Spotlight code to search your own index from inside your app.

All the work is done using the CSSearchQuery class, which works asynchronously. You need to give it two closures to work with: one to call when it finds a matching item (which should append the item to a results array), and one to call when the search finishes, at which point you should update your UI with the search results.

CSSearchQuery works similarly to Core Data – it even has the same approach to specifying search criteria. In this example we’re going to search for "contentDescription == \"*\(text)*\"c", which means “find things that have a contentDescription value equal to any text, followed by our search text, then any text, using case-insensitive matching.

There are a few more things you need to know before I show you the code:

  1. Running a CSSearchQuery returns CSSearchableItem items, so we need to an array to store that data type.
  2. We’ll be taking advantage of closure capturing to share that array between the “found items” closure and the “search is finished” handler.
  3. Your closures can be called on any thread, so as you usually manipulate the UI when the search finishes you should push that work to the main thread.
  4. You need to explicitly call start() on the search to make it begin.
  5. In case a user types really fast, we want to a way to cancel the existing search before starting a new one. To make that happen, it’s a good idea to store the CSSearchQuery object as a property in the class, then call cancel() on it before searching.

To try out the code below, add import CoreSpotlight to a view controller’s class, then give it a CSSearchQuery? property called searchQuery.

Now add this method:

func runSearch(text: String) {
    var allItems = [CSSearchableItem]()


    let queryString = "contentDescription == \"*\(text)*\"c"
    searchQuery = CSSearchQuery(queryString: queryString, attributes: nil)

    searchQuery?.foundItemsHandler = { items in
        allItems.append(contentsOf: items)

    searchQuery?.completionHandler = { error in
        DispatchQueue.main.async { [unowned self] in
            self.updateUI(matches: allItems)


You’ll need to implement updateUI() to do something with your search results, such as updating a table view.

Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 10.0 – learn more in my book Advanced iOS: Volume One

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Link copied to your pasteboard.