StartWorkoutIntent
An App Intent for starting a workout.
Declaration
protocol StartWorkoutIntent : InstanceDisplayRepresentable, SystemIntentMentioned in
Overview
On Apple Watch Ultra, this intent registers a start workout action for the Action button. After installing your app, the user can select this action in Settings > Action Button by setting Action to Workout and App to your app.
When implementing this intent, define the following:
A title property
A workoutStyle property
A list of suggestedWorkouts
A displayRepresentation property
A perform() method
struct MyStartWorkoutIntent: StartWorkoutIntent {
// Define the intent's title.
static var title: LocalizedStringResource = "Start Workout"
// Define a list of start workout intents that appear below the First Press
// settings when someone sets your app as the workout app in
// Settings > Action Button.
static var suggestedWorkouts: [MyStartWorkoutIntent] = [MyStartWorkoutIntent()]
// Define a parameter that specifies the type of workout that this
// intent starts.
@Parameter(title: "Start Workout Entity")
var workoutStyle: WorkoutEnum
// Define an init method that sets the default workout type.
init() {
workoutStyle = .workout
}
// Set the display representation based on the current workout style.
var displayRepresentation: DisplayRepresentation {
WorkoutEnum.caseDisplayRepresentations[workoutStyle] ??
DisplayRepresentation(title: "Unknown")
}
// Launch your app when the system triggers this intent.
static var openAppWhenRun: Bool { true }
// Define the method that the system calls when it triggers this event.
func perform() async throws -> some IntentResult {
let workoutManager = MyWorkoutManager.shared
await workoutManager.requestAuthorization()
await workoutManager.startWorkout()
return .result()
}
}For more information, see Responding to the Action button on Apple Watch Ultra.