We have now seen how to integrate both Spotlight search continuation and Spotlight index querying in to our apps. The completionHandler is called on an arbitrary background queue, so make sure to synchronise on to a queue that is appropriate to your caller (synchronising to the calling queue can be achieved using the OperationQueue API).įinally, we call the start() function on our newly created query, which kicks off the search on the Spotlight database. Having checked for any errors, we call the completion closure passed in to the function. This handler is not called if the query is cancelled, so you only need to handle the query completion case in this handler. Having done this, we store the counties in our searchResults property.Īfter this we have the completionHandler of the CSSearchQuery, which as its name implies, is called when the search completes. In this handler you have a chance to map these items back to your application’s domain model, which in our case is a list of counties. This is called with the aforementioned CSSearchableItem instances that the Spotlight index returns as a result of your query. The next thing is the CSSearchQuery object’s foundItemsHandler. As in this example we only need the uniqueIdentifier from the CSSearchableItem instances that the query will return, we don’t need to specify any attributes here. We can also specify a particular set of attributes to be returned by the query, which helps to limit the amount of data to be returned from the Spotlight database, improving the search speed.
For our purposes, we will simply create a query string that escapes quotes and backslashes and uses appropriate comparison modifiers ( see the Github repo for the exact implementation). There is a detailed query language that can be used to create powerful queries, which is documented in the CSSearchQuery API Reference. Next we create a new query, constructed using the given search string.
If the user is typing in to the search field as a query is executing we don't want any results from the previous search to be returned cancelling the current query ensures that this doesn’t happen. The first thing we do is cancel any currently running query. Let’s break it down and see how it works. It is passed the user’s query string and a completion handler to be called when the search has finished. This is the function we call on our search controller whenever the user wants to perform a search. Continue Spotlight Searches in Your AppĪ somewhat under-advertised feature of iOS 10’s Spotlight search is that searches can now be continued inside apps. Spotlight had been opened up, but we were only allowed a peek in to it. The second was that while users could perform a simple text search in the Spotlight UI to find your content, there was no way to extend this functionality to allow for more complex queries. You could put data in to it, but there was no way to get anything back out again. The first was that the Spotlight index was a complete black box. There were, however, a couple of glaring omissions from this round of updates. By allowing your application’s data to be indexed, you can increase engagement with your app by allowing users to find their content in one convenient location. An updated version of this article can be found on īack when iOS 9 was released, Apple made great advances to make the system Spotlight search more useful to users by allowing third-party apps to integrate their data with the Spotlight database (for more details, see Indexing App Content with Core Spotlight).