How to open app when an indexed entity is tapped in Spotlight?

Asked on 08/07/2024

1 search

To open an app when an indexed entity is tapped in Spotlight, you need to follow these steps:

  1. Conform to the IndexedEntity Protocol: First, ensure that your entity conforms to the new IndexedEntity protocol. This allows your app's entities to be indexed and searchable in Spotlight.

  2. Index Your Entities: In your app's initialization method, index your entities using CSSearchableIndex. For example, you can call indexAppEntities on CSSearchableIndex to index all your entities.

  3. Implement an Intent: Create an intent that can handle the entity. For instance, if you have a trail entity, you might have an OpenTrailIntent that accepts a trail entity as a parameter.

  4. Search and Tap: When a user searches for an entity in Spotlight and taps on the result, the intent you implemented will be triggered, and the app will open to the relevant content.

Here is a more detailed example from the session "What’s new in App Intents":

"First, I'll add conformance to the new IndexedEntity protocol to my trail entity. Then in my app's init method, I'll index all the trail entities from my data manager by calling indexAppEntities on CSSearchableIndex. And that's it. I'm now donating my trail entity content to Spotlight. Let's see it in action. First, I'll launch my app so the new entity indexing code has a chance to run. Now I'll search for one of my trails in Spotlight. We can see that my trail content is showing up as a search result, and because I have an open trail intent that accepts our trail entity as a parameter, I can tap on the result to go straight to it in the app."

For more information on integrating your app with Spotlight, you can refer to the chapter on "Spotlight integration" in the session "What’s new in App Intents".