SnippetRepresentableEntity
An app entity that provides a visual representation for its content.
Declaration
protocol SnippetRepresentableEntity : AppEntityOverview
Adopt the SnippetRepresentableEntity protocol in your app entity when you want to provide a visual representation of its content. This protocol conforms to the base AppEntity protocol and adds a snippet property for delivering a snippet type to display your entity’s content. When the system needs to show information about your entity, it displays the contents of your snippet. For example, Siri might show the snippet during a conversation to confirm a choice or display details about a requested item.
Implement the snippet property in your entity type and return a snippet with the SwiftUI view you want the system to display. In the following example, the property returns a CardSnippet type, which delivers the custom MessageSnipperCard view from the app. This view uses the provided message object to configure the views it displays.
extension MessageEntity: SnippetRepresentableEntity {
static var snippet: some EntitySnippetContent<MessageEntity> {
CardSnippet(supportedFamilies: [.standard]) { message, _ in
// Prepare any extra data before returning the view.
MessageSnippetCard(message: message)
}
}
}When you build the content for your snippet, create views with primarily static content such as text and images. The system renders snippets in advance and stores your views in a static archive it can use at any time, including when your app isn’t running. Because rendering happens in advance, you can’t change the content of your view dynamically later. If your view requires data that isn’t immediately available in your entity, prepare the data before returning the SwiftUI view from your snippet. For example, if you need to create custom images, create them before you return your view. Prepare data synchronously to ensure it’s available when you create your view. If you generate data asynchronously, it likely won’t be available in time.
To include interactive controls in your view, add a SwiftUI control that runs an app intent. For example, you can initialize a Button or Toggle view with an app intent. When someone interacts with one of your controls, the system runs the specified app intent. After your app intent runs, the system generates a new SwiftUI view archive for your snippet, incorporating any changes you made to your views because of the app intent.