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

Generated on 8/7/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 by Spotlight.

  2. Index Your Entities: In your app's initialization method, index all the entities using CSSearchableIndex. This can be done by calling indexAppEntities on CSSearchableIndex.

  3. Implement an Intent: Create an intent that can handle the entity. For example, 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 the entity in Spotlight and taps on the result, the intent will be triggered, and the app will open to the relevant content.

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

  • Indexing the Entity:

    // Add conformance to IndexedEntity protocol
    struct Trail: IndexedEntity {
        // Implementation details
    }
    
    // Indexing entities in the app's init method
    let trails = dataManager.getAllTrails()
    CSSearchableIndex.default().indexAppEntities(trails)
    
  • Handling the Tap:

    • Ensure you have an intent that can handle the entity. For example, OpenTrailIntent:
    struct OpenTrailIntent: AppIntent {
        @Parameter(title: "Trail") var trail: Trail
    
        func perform() async throws -> some IntentResult {
            // Open the app to the specific trail
        }
    }
    

When the user taps on the search result in Spotlight, the OpenTrailIntent will be triggered, and the app will navigate to the specific trail.

For more details, you can refer to the session "What’s new in App Intents".

What’s new in App Intents

What’s new in App Intents

Learn about improvements and all-new features with App Intents, and discover how this framework can help you expose your app’s functionality to Siri, Spotlight, Shortcuts, and more. We’ll show you how to make your entities more meaningful to the platform with the Transferable API, File Representations, new IntentFile APIs, and Spotlight Indexing, opening up powerful functionality in Siri and the Shortcuts app. Empower your intents to take people deep into your app with URL Representable Entities. Explore new techniques to model your entities and intents with new APIs for error handling and union values.

Bring your app to Siri

Bring your app to Siri

Learn how to use App Intents to expose your app’s functionality to Siri. Understand which intents are already available for your use, and how to create custom intents to integrate actions from your app into the system. We’ll also cover what metadata to provide, making your entities searchable via Spotlight, annotating onscreen references, and much more.

Platforms State of the Union

Platforms State of the Union

Discover the newest advancements on Apple platforms.

Bring your app’s core features to users with App Intents

Bring your app’s core features to users with App Intents

Learn the principles of the App Intents framework, like intents, entities, and queries, and how you can harness them to expose your app’s most important functionality right where people need it most. Find out how to build deep integration between your app and the many system features built on top of App Intents, including Siri, controls and widgets, Apple Pencil, Shortcuts, the Action button, and more. Get tips on how to build your App Intents integrations efficiently to create the best experiences in every surface while still sharing code and core functionality.

Support semantic search with Core Spotlight

Support semantic search with Core Spotlight

Learn how to provide semantic search results in your app using Core Spotlight. Understand how to make your app’s content available in the user’s private, on-device index so people can search for items using natural language. We’ll also share how to optimize your app’s performance by scheduling indexing activities. To get the most out of this session, we recommend first checking out Core Spotlight documentation on the Apple Developer website.