ShowInAppSearchResultsIntent
An app intent that displays a set of search results in the app’s interface.
Declaration
protocol ShowInAppSearchResultsIntent : SystemIntentMentioned in
Overview
The system uses this protocol to route search requests for your app’s entities to your app. If your app has a search interface, implement this protocol in a custom type and use it to display the entities that match the provided search criteria. If your app supports multiple entity types and displays results for each of them differently, provide separate implementations of this type for each entity.
In your custom type, specify the search criteria you support and the search scope for your app’s content. For most implementations, use the StringSearchCriteria type to match the provided string against the text found in your app’s entities. For most entity types, include the StringSearchScope.general option in the searchScopes property. If your content includes media, update the search scopes to reflect the type of content your app includes. Use your custom type’s perform() method to run the search and display the results in your app’s search interface.
The following example shows an implementation of this protocol that uses a string-based search term to locate items. The perform() method fetches the string value and passes it to an app-specific type responsible for performing the search and displaying the results.
struct MySearchIntent: ShowInAppSearchResultsIntent {
static let title: LocalizedStringResource = "Search my entities."
static let searchScopes: [StringSearchScope] = [.general]
@Parameter var criteria: StringSearchCriteria
@Dependency var searchManager: SearchManager // A custom app object.
func perform() async throws -> some IntentResult {
let searchTerm = criteria.term // Get the string value to match against.
searchManager.displayResults(searchTerm)
return .result()
}
}This app intent needs to run from your app, and not from your app extension. If you implement your app intent code in a shared framework, make sure the allowedExecutionTargets property includes your app. The protocol also provides a default implementation of the supportedModes property that includes the foreground runtime.