// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Indexing App Content with Core Spotlight

Index your iOS app's data with Core Spotlight so people can find it in system search, then search it inside your app with the iOS 10 CSSearchQuery API.

Art+Logic Jan 23, 20173 min read

From the Art+Logic archive · written in 2017. The tools may have moved on; the thinking still holds.

Indexing App Content with Core Spotlight

Core Spotlight has been available since iOS 9 but Apple has made some updates in iOS 10 that make it worth another look. In this series of posts I’ll look at how you can use Core Spotlight to index your application data and search that data both from within iOS using Spotlight and from within an app using some of the new Core Spotlight APIs such as CSSearchQuery.

Our Demo App

To demonstrate how to index application data using Core Spotlight I’ve created a basic iOS app that loads and displays information about some airports and heliports around the world. The app loads this data from a CSV file bundled within the app, displays a list of airports, and provides a basic detail view showing the airport on a map along with the type of airport and identification code. The source to the app is available on GitHub.

Airports Demo app
Airports - a demo app that displays some basic information about airports.

Indexing Application Data with Core Spotlight

After loading the airport data from the bundled CSV the app adds information about each airport to the on-device Spotlight index. The indexAirportData method of the ALAirportController class takes care of this for us:

indexairportdata.mindexairportdata.m

- (void)indexAirportData
{
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
      for (ALAirport* airport in self.airports)
      {
         // Create the attribute set
         CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*)kUTTypeItem];
         attributeSet.displayName = airport.airportName;
         attributeSet.latitude = @(airport.coordinate.latitude);
         attributeSet.longitude = @(airport.coordinate.longitude);
         attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:@"AppIcon"]);
         // Create the searchable item
         CSSearchableItem* item = [[CSSearchableItem alloc] initWithUniqueIdentifier:airport.airportIdentifier
                                                                    domainIdentifier:@"airports"
                                                                        attributeSet:attributeSet];
         [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item]
                                                        completionHandler:^(NSError * _Nullable error) {
                                                           NSLog(@"Indexed: %@", airport.airportName);
                                                        }];
      }
   });
}

We first create an CSSearchableItemAttributeSet instance for each airport and set the displayName, latitude, longitude, and thumbnailData attributes. For now, when searching I’ll rely on the displayName attribute, which is set to the name of the airport. The thumbnailData image data will be displayed by iOS when searching using Spotlight. Here I’m simply using the app icon, but if your content has an associated image you could provide it instead. Finally, I include the location of the airport in case we want to support searching by location in a later update.

Next, we create an CSSearchableItem using the attribute set we just created along with a unique identifier. In this case, I’m simply using the airport identification code. When a user taps on a Spotlight search result for your app’s data you’ll get this identifier as part of the data passed to your app delegate.

Finally, we ask the CSSearchableIndex class to index the item we created and log the airport name when it’s done indexing.

Responding to Spotlight Queries

Here we can see a Spotlight search in action. As I start typing a query into the Spotlight search field, results from within the app are presented to the user.

Animated GIF of an example Spotlight search.

When I tap on one of the search results our UIApplicationDelegate will receive an application:continueUserActivity:restorationHandler message. The NSUserActivity instance passed to this method, as part of its userInfo dictionary, will contain the unique identifier for the CSSearchableItem that we created and indexed earlier. Your app can use that identifier as appropriate. Here we’re using it to display the detail view of the related airport.

What’s Next?

In Part 2 we’ll look at how we can use the CSSearchQuery API introduced in iOS 10 to search the Spotlight index of our airport data from within our demo app.


Header image via https://www.flickr.com/photos/blondinrikard/, licensed under CC.

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…