Track workouts with HealthKit on iOS and iPadOS

Asked on 07/01/2025

1 search

To track workouts with HealthKit on iOS and iPadOS, you can utilize the powerful workout APIs provided by HealthKit. Here's a brief overview of the process:

  1. Run a Workout Session: You can use a workout session to track any activity on iPhone and iPad, similar to how it's done on Apple Watch. This involves creating a workout configuration, setting the activity type (e.g., running), and using the associated workout builder to save the workout in HealthKit. You can start by creating a workout configuration and setting its type to reflect the activity the person will perform, such as running, and set the location to outdoor. Then, create an HK workout session, pass in the configuration, and attach a data source. Call Prepare on the session and display a countdown before starting the workout. Track workouts with HealthKit on iOS and iPadOS.

  2. Get Session Metrics: During the workout, you can collect various health metrics. HealthKit allows you to pair with external devices like heart rate monitors to gather data. You can add these measurements as samples to the health database, which will automatically be passed to your live builder. Track workouts with HealthKit on iOS and iPadOS.

  3. Recover from a Crash: In case of a crash, you can handle active workout recovery by using the app delegate to check if recovery is needed and continue the workout session. Track workouts with HealthKit on iOS and iPadOS.

  4. Best Practices: Always use the Workout Builder API to create and save workouts to ensure the activity rings are updated. Request authorization only for the data types you need to avoid confusing users. Track workouts with HealthKit on iOS and iPadOS.

These steps will help you effectively track workouts using HealthKit on iOS and iPadOS.