Search code examples
iosswifthealthkit

how to get totalDistance of HKWorkout


At the moment I am working on a running app for the iPhone.

One issue that I have is, that I am building the HKWorkout using the HKWorkoutBuilder according to Apples Documentation.

But I am not able to associate the totalDistance with the workout – mentioned here in the Documentation. totalDistance is nil when I try to read it from the workout – no surprise because I didnt set it. As I understand the HKWorkoutBuilder provides no way to add the totalDistance.

I think I miss something here.


Solution

  • Add your distance samples to HKWorkoutBuilder using its addSamples method. The total distance will be calculated for you based on the samples you add. e.g you can add samples of type HKQuantityTypeIdentifierDistanceWalkingRunning to the workout and it should populate totalDistance.