Track the user’s activities on Apple Watch.
Workout sessions let you track a user’s activity on Apple Watch. While the session is running, the system fine tunes the watch’s sensors for the specified activity. For exampe, all workout sessions generate high-frequency heart rate samples; however, an outdoor cycling activity generates more accurate location data, while an indoor cycling activity does not.
To run a workout session:
Start the workout session on Apple Watch.
End the workout session on Apple Watch.
Create and save the workout samples.
Since health data is synced between Apple Watch and iPhone, feel free to create and save the workout samples on the device that makes the most sense for your users. However, If there is a workout in progress, both devices should display current information about the workout.
Starting Workout Sessions
Start a workout session on Apple Watch. Be sure to pick the workout activity type and location that optimizes the accuracy of Apple Watch’s calorimetry, based on the type of workout the user is performing.
HealthKit saves active energy burned samples to the HealthKit store while the workout is running. The calories are calculated based on the activity and location type chosen. HealthKit provides customized calorie calculations for run, walk, cycle, stair climbing, elliptical, and rowing activities. Furthermore, the calculations for run, walk, and cycle activities distinguish between indoor and outdoor locations.
The system calculates the calories for all other activities as “other.” Calories are calculated at a rate no lower than a brisk walk, but may be higher, based on the data from Apple Watch’s sensors.
Ending the Workout Session
As soon as the workout is over, stop the workout session on Apple Watch. Starting and stopping the workout session ensures that the workout contributes to the Exercise ring. The system updates the exercise ring based on the amount of time the user spent actually exerting themselves during the session, as calculated by the watch’s sensors.
Creating and Saving Workout Samples
Create active energy burned samples to represent the calories burned during the workout session. Alternatively, if you want HealthKit to calculate the calories, query for all the active energy burned samples saved by Apple Watch during the workout.
Next, create an
HKWorkout object to represent the workout session. When creating the workout, be sure to do the following:
Choose an activity type that best describes the activity that the user actually performed. This is the name that appears in the Activity app.
Set the workout object’s total energy to the sum of all the workout’s active energy burned samples.
For walk, run, or cycle activity types, use the
HKMetadatametadata key to properly mark the exercise as either indoor or outdoor.
Key Indoor Workout
Finally, associate the active energy burned samples with the workout. You may also want to create and associate other samples with the workout.
Workouts saved on Apple Watch always contribute to the Move and Exercise rings. In iOS 10 and later, workout samples saved on iPhone also contribute to these rings. However, unlike workout objects saved on Apple Watch, the iPhone does not try to calculate the actual amount of time spent exercising. Instead, the system simply increases the Exercise ring by the workout’s total duration.
Following Best Practices
To create the best user experience, please consider the following guidelines:
The iOS app can use a
WCSessionobject to determine whether the user has a paired Apple Watch, and whether your corresponding Watch app is installed. If your Watch app is installed on a paired Apple Watch, you can launch the Watch app from your iOS app by calling the health store’s
Watch App(with: completion:)
When starting a workout in your watchOS app, use the Watch Connectivity Framework to inform your iOS app about the workout, and to update the iOS app’s UI about the current state of the workout.
If the user starts the workout in your watchOS app, and then tries to end it in your iOS app, the iOS app should instruct the user to end the workout in your watchOS app. Otherwise, Apple Watch workout continues to run, which can lead to accidentally saving invalid data to the HealthKit store.
If the user starts a workout in your iOS app, and then opens your watchOS app, the watchOS app should automatically start a workout session for the workout in progress. If you save the workout in the watchOS app, you can incorporate data from the iOS workout. For example, you should set the workout’s
startto the iOS workout’s start. Also, if your app calculates its own calories, you can retroactively give credit for the calories burned before the workout session began.
If the user does not start and stop a workout session in your watchOS app, do not try to retroactively create a workout on Apple Watch.
Make starting and stopping workouts on Apple Watch as easy and obvious as possible. The app must clearly indicate when a workout session is in progress, and then either automatically save workout data for the user, or provide a clear option to explicitly save or discard workout data. Finally, the watchOS app should provide clear feedback when a workout is successfully saved. For more information, see Health and Fitness in watchOS Human Interface Guidelines.