When I tap on an indexed entity in Spotlight, how can I open my app to a view which displays detail of the entity?

Generated on 8/7/2024

1 search

To open your app to a view that displays the details of an entity when you tap on an indexed entity in Spotlight, you can use the new indexedEntity protocol and the open intent. Here’s a step-by-step guide based on the information from the WWDC sessions:

  1. Conform to indexedEntity Protocol:

    • First, make your entity conform to the indexedEntity protocol. This allows your entity to be indexed by Spotlight.
    • Example:
      struct Trail: indexedEntity {
          // Your entity properties and methods
      }
      
  2. Index Your Entities:

    • In your app’s initialization method, index all your entities using CSSearchableIndex.
    • Example:
      let trails = dataManager.getAllTrails()
      CSSearchableIndex.default().indexAppEntities(trails)
      
  3. Implement attributeSet:

    • Customize the attributeSet to include relevant properties that Spotlight can use to display and search your entity.
    • Example:
      extension Trail {
          var attributeSet: CSSearchableItemAttributeSet {
              let attributes = CSSearchableItemAttributeSet(itemContentType: .content)
              attributes.title = self.name
              attributes.keywords = self.keywords
              // Add more attributes as needed
              return attributes
          }
      }
      
  4. Create an Open Intent:

    • Define an intent that will handle opening the entity in your app.
    • Example:
      struct OpenTrailIntent: AppIntent {
          static var title: LocalizedStringResource = "Open Trail"
          @Parameter(title: "Trail") var trail: Trail
      
          func perform() async throws -> some IntentResult {
              // Code to navigate to the trail detail view
          }
      }
      
  5. Handle the Tap Action:

    • When a user taps on the indexed entity in Spotlight, the open intent will be triggered, and your app will navigate to the appropriate view.
    • Example:
      func perform() async throws -> some IntentResult {
          // Use your app's navigation manager to open the trail detail view
          navigationManager.openTrailDetail(trail)
          return .result()
      }
      

For more detailed information, you can refer to the session "What’s new in App Intents" (02:35) which covers the process of indexing entities and handling Spotlight search results.

Relevant Sessions

  1. What’s new in App Intents
  2. Bring your app’s core features to users with App Intents
  3. Bring your app to Siri
  4. Support semantic search with Core Spotlight
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.

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.

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.