A workout sample that stores information about a single physical activity.
- iOS 8.0+
- Mac Catalyst 13.0+
- watchOS 2.0+
You don’t need a specific type identifier to create the
HKWorkoutinstance. All workouts use the same type identifier.
You must provide an
HKWorkoutvalue for each workout. This value defines the type of activity performed during the workout.
After saving the workout to the HealthKit store, you must associate additional samples with the workout (for example, active energy burned or distance samples). These samples provide fine-grained details. Use the
add(_:method to associate them with the workout.
The workout records a summary of information about a single physical activity (for example, the duration, total distance, and total energy burned). It also acts as a container for other
HKSample objects. You can associate any number of samples with a workout, adding details over the course of the workout. For example, you may want to break a single run into a number of shorter intervals, and then add samples to track the user’s heart rate, energy burned, distance traveled, and steps taken for each interval. For more information, see Adding Samples to a Workout.
HealthKit supports a wide range of activity types. For a complete list, see
Workouts are mostly immutable. You set their properties when you instantiate the workout, and they can’t change. However, you can continue to add samples to the workouts.
Fill the Activity Rings
Workouts can contribute to the Move and Exercise rings in the Activity app. To affect the rings, you must associate one or more active energy burned samples with the workout. Additionally:
In watchOS. Use a workout session to track the user’s activity. When the session has ended, create a workout object and the associated active energy burned samples. For more information, see
The system updates the Move ring based on the active energy burned samples. It updates the Exercise ring based on the amount of time the user spent actually exerting themselves during the workout session, as calculated by the watch’s sensors.
In iOS. No additional work is necessary. Workout objects automatically contribute to both the Move and Exercise rings. The Exercise ring increases by the workout’s total duration, and the Move ring increases by the number of calories in the associated active energy burned samples. HealthKit also increases the Stand ring by one hour for each wall-clock hour that the workout overlaps.
Create and save workouts on the device that makes the most sense for your application—typically the device processing the user’s workout.
Like many HealthKit classes, the
HKWorkout class should not be subclassed. You may extend workouts by adding metadata with custom keys as appropriate for your app.
For more information, see the methods