Understanding Spotlight search

If you have a Mac, which you most likely do, you have probably used the blazingly fast Spotlight search feature on it. Furthermore, if you love this feature on your Mac, you have probably used it on iOS as well. Spotlight is a highly optimized search engine that enables you to find content throughout your device.

If you haven't used Spotlight, try swiping down on your home screen on iOS and use the search box. Or, on your Mac, press command + space to open the Spotlight search dialog.

Spotlight has been around since iOS 3. However, it wasn't until iOS 9 that Apple opened up Spotlight to developers. This means that starting with iOS 9, Spotlight indexes everything from web results, apps, app store results, and any content you add to the Spotlight index yourself. Spotlight presents search results with an image, a title, and some extra information, and sometimes extra buttons are shown to make a phone call or to start turn-by-turn navigation to an address.

The results that are displayed by Spotlight are a mix of public or online content and private results. If your app has indexed items that are publicly available, Apple will make them visible to all users who have your app installed as long as enough people interact with this particular searchable item.

A great example of public content for almost any app is the main navigation items in an app. Imagine you have an app that has a couple of tabs in UITabBarController. Any user that downloads your app and opens it can immediately access each tab if they want to. This means that these tabs are public; any user of your app can access these tabs.

Your app can index these tabs using CoreSpotlight and mark them as publicly available. When you mark an item as public, it will not be globally visible right away. Behind the scenes, CoreSpotlight indexes the item as it usually would when indexing a private record. Whenever a user selects this particular item from a list of Spotlight search results, it sends an irreversible hash of the item to Apple's servers. If Apple receives the same hash over and over again, a certain threshold will be reached. Once this threshold is reached, the indexed item is pushed to all people who have your app installed. If your indexed item has a public web resource associated with it, your publicly searchable item will even be shown to people who don't have your app installed because the content is available on the web. If a user selects this item but doesn't have your app installed, they will be taken to your website instead to view the requested content there.

The fact that public results in Spotlight are kept private for a while helps to make sure that everything your users see when they use Spotlight is relevant. Imagine that any app developer could mark any content as public and every time a developer does this, the item is pushed directly to all users. This would pollute Spotlight, and the entire feature would become virtually worthless. Spotlight's goal is to provide relevant and interesting results. The threshold for interactions makes sure that enough people consider the displayed item to be appropriate for their search query, which means that lousy indexing or spam doesn't have a chance to thrive.

Another reason this threshold exists is to protect your user's data. It's perfectly fine to add personal and private data to Spotlight. The standard mail app does this so users can search their email messages using Spotlight. These email messages should not be publicly indexed so they shouldn't be marked as such. Mistakes happen though, and in case you do end up marking private data as public by accident, there is no way this specific item will show up for other users because the public result threshold for the item will never be reached since only one user has interacted with the Spotlight search result.

Excellent Spotlight visibility can boost usage of your apps, especially since users don't even need to have your app installed to see results for your app as long as there is a web resource available for your indexed contents. Apple has worked hard to ensure that Spotlight is fast and provides a seamless experience for your users. This means that users won't notice whether a result comes straight from their device or whether they see a result because it was publicly indexed.

You might have noticed that Safari sometimes shows results for in-app content. These results come from Spotlight. Whenever you add a web resource to an item you index, Safari will show this resource if it matches a user's query in the search bar. In this chapter, you will learn all about optimally indexing your app's contents; so, if you have an app that has a website that mirrors its contents, you'll know how to maximize your presence in search results for both Spotlight and Safari.