Every iOS version brings something new and also iOS 9 could not just stay out of this tradition, so developers have new APIs to push their apps to higher levels. When you swipe down, Spotlight throws four app suggestions at you in addition to the search bar. Swipe right on the home screen and you'll see a more fully rounded Spotlight search screen with app suggestions along with suggested contacts, local search suggestions, and news tailored to your interests. One of the most important additions is the ability for apps to communicate with Spotlight so your content can be shown in the Spotlight. Those app suggestions are based on the new intelligence features of iOS 9 and represent apps you've recently used, apps you frequently use, or apps that you often use at this time of day or in this place. Tapping any of them will launch that app. Through Settings application you can easily control what appears in your search results by disabling apps from showing up when you search. Being able to deep link into app is great. Found a farm you like on the CFI app? Well now Spotlight search can take you right back to that result if you search for the name of the farm.
What we’ve done
Core Spotlight is the best way to provide full search capabilities of app’s private content. This framework has a database-style design and lets you provide even more information about the content that you want to be searchable. Using Core Spotlight means importing two extra frameworks:
- import CoreSpotlight
- import MobileCoreServices
The CSSearchableItemAttributeSet class helps you support actions that users can take from an item in search results; specifically, you can let users make a phone call or get directions to a location. In our case, we need to look inside the farms array, create a CSSearchableItemAttributeSet object from array’s elements. This attribute set can store lots of information. When initializing CSSearchableItemAttributeSet, an itemContentType parameter is required.
We also need to wrap it to a CSSearchableItem object, which contains a unique identifier and a domain identifier. CSSearchableItem object represents the item and add it to the on-device index. Unique identifier must specify the item absolutely uniquely inside your app, but domain identifier groups items together. The unique identifiers are the only information that we receive from the operating system when our search result is selected by the user. You need to use these identifiers to restore you back to the correct state.
To index an item, you need to call method indexSearchableItems on the default searchable index of CSSearchableIndex, passing in an array of CSSearchableItem objects. This method runs asynchronously, so we're going to use a trailing closure to be told whether the indexing was successful or not.
We assign the farm name to the title property of the attribute set. This title is what will appear at the top of our search results. Next, we create a descriptive string and assign this to the contentDescription property of our searchable attribute set. This string will be displayed below the result's title in Spotlight. To help users get directions to an farm location described in a search result, set the item’s supportsNavigation property to 1 and supply values for the latitude and longitude properties.
When the user taps on a searchable item from your app in Spotlight search results, your app delegate’s application:continueUserActivity:restorationHandler: method is called (this is the same method you implement to support Handoff). The activity type your app delegate receives is CSSearchableItemActionType, which helps you determine the app content to show the user. In your implementation of this method, check the type of the incoming activity to confirm that your app is opening because the user tapped an indexed item in a search result. If everything works fine, you should display correct view controller to the user. Keep in mind, Apple has said that iOS automatically monitor how often users interact with your search results, and if you provide unhelpful results then your results may stop appearing. Index only what's important!
The new version of our app will look like this
Want to check out the app we referenced in this blog? Click below to download!