AppIntent
AppIntentManager is used to register and manage AppIntents in the Scripting app. It serves as the core mechanism for executing script logic behind controls in Widgets, Live Activities, and ControlWidgets.
All AppIntents must be defined in the app_intents.tsx file. When an intent is executed, the script runs in the "app_intents" environment (Script.env === "app_intents").
Once registered, these intents can be triggered by Button and Toggle controls within Widgets, Live Activities, or ControlWidgets, allowing users to define interactive behavior via script.
1. Type Definitions
AppIntent<T>
Represents a concrete intent instance with parameters and metadata.
AppIntentFactory<T>
A factory function that creates an AppIntent instance with specified parameters.
AppIntentPerform<T>
A function that handles intent execution logic asynchronously.
AppIntentProtocol
An enumeration that defines the behavior type of the intent.
2. AppIntentManager Class
AppIntentManager.register<T>(options): AppIntentFactory<T>
Registers a new AppIntent by specifying its name, protocol, and perform logic. When a control (e.g., Button or Toggle) triggers the intent, the associated perform function is called.
Parameters:
Returns:
AppIntentFactory<T>: A factory function that creates anAppIntentinstance with the specified parameters.
Example:
In a control view file (e.g., control_widget_toggle.tsx):
In a widget file (widget.tsx):
3. Execution Environment
All AppIntents registered via AppIntentManager are executed in the "app_intents" environment.
This allows safe use of APIs suitable for background execution, such as:
- Fetching data from the network
- Controlling Live Activities
- Triggering control view refreshes
4. Best Practices
-
Centralized Definitions: All AppIntents must be defined in
app_intents.tsxfor discoverability and maintainability. -
Strong Typing: Define explicit parameter types
Tfor bothperformand control usage to benefit from type checking and autocomplete. -
Choose the Right Protocol:
- General operation →
AppIntent - Audio playback →
AudioPlaybackIntent - Audio recording →
AudioRecordingIntent(requires iOS 18+, with Live Activity) - Live Activity control →
LiveActivityIntent
- General operation →
-
Trigger UI Updates: If the intent modifies a UI state (e.g., toggle), call:
ControlWidget.reloadButtons()ControlWidget.reloadToggles()Widget.reloadAll()depending on where the control is hosted.
