// 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

Searching App Content with Core Spotlight

Part 2 of the Core Spotlight series: search your app's Spotlight index from inside the app with the iOS 10 CSSearchQuery class, with a demo on GitHub.

Art+Logic Jan 30, 20172 min read

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

Photo by tyler hendy from Pexels

In the first part of this series of posts about Core Spotlight we looked at how to use the Core Spotlight APIs to index application data, how to search that data using Spotlight, and how to respond when a user selects a search result for your application’s data. In this post we’ll take a look at how easy it can be to search that same index from within your app. The demo app has been updated and is still available on GitHub.

Searching App Content with Core Spotlight

Using CSSearchQuery to Search the Spotlight Index

In iOS 10 Apple introduced the CSSearchQuery class that allows us to search the same Spotlight index from within our app that we relied on in the first post when searching from the iOS Spotlight search field. The class is straightforward to use - you basically:

  1. Define the query using the straightforward search query syntax.
  2. Define handler blocks that are called when items are found or the search has completed.
  3. Start the search.

In the searchFor: method below, extracted from the AirportsViewController class of the demo app, you can see this process in action.

At the moment, it’s not possible to update an existing query so in the demo app as the user enters text into the search field we cancel any existing queries and create a new one with each change made to the search query. Despite that, if you’re taking advantage of Core Spotlight to index content within your app, the CSSearchQuery API makes it trivial to search that content, and is worth checking out in iOS 10.

search.m

- (void)searchFor:(NSString*)query
{
   // Cancel any existing search queries
   if (self.searchQuery)
   {
      [self.searchQuery cancel];
      self.searchQuery = nil;
      [self.searchResults removeAllObjects];
      [self updateSearchResults];
   }
   // Create the query
   NSString* queryString = [NSString stringWithFormat:@"displayName == '*%@*'c", query];
   self.searchQuery = [[CSSearchQuery alloc] initWithQueryString:queryString
                                                      attributes:@[@"displayName"]];
   __weak typeof(self) weakSelf = self;
   // When items are found, add them to the search results
   self.searchQuery.foundItemsHandler = ^(NSArray<CSSearchableItem *> *items) {
      for (CSSearchableItem* item in items)
      {
         ALAirport* airport = [weakSelf.airportController
                               airportWithIdentifier:[item uniqueIdentifier]];
         [weakSelf.searchResults addObject:airport];
         [weakSelf updateSearchResults];
      }
   };
   // When complete, update the search results
   self.searchQuery.completionHandler = ^(NSError* error) {
      if (error)
      {
         NSLog(@"%@", error);
      }
      else
      {
         [weakSelf updateSearchResults];
      }
   };
   // Start the search. Handle results and errors using the handler blocks
   [self.searchQuery start];
}

Header image by Aidan Gibbons, licensed under Creative Commons

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…