7

I am using HKLiveWorkoutBuilder & HKWorkoutRouteBuilder to track a workout on Apple Watch.

when calling:

[workoutBuilder finishWorkoutWithCompletion:^(HKWorkout * _Nullable workout, NSError * _Nullable error) {

}];

The returned workout object does not include the distance that the user travelled:

(lldb) po workout.totalDistance
nil

However the route is saved correctly.

Given that .totalDistance is read only, how should I set the distance of the workout in watchOS 5?

enter image description here

enter image description here

lewis
  • 2,531
  • 2
  • 32
  • 56

1 Answers1

3

There are two parts to the answer here.

To get it working for walking or running you can use the following code:

workoutBuilder?.beginCollection(withStart: Date(), completion: { (success, error) in
    guard success == true else {
        #warning ("deal with failure")
        return
    }

    workoutBuilder?.dataSource = HKLiveWorkoutDataSource(healthStore:self.healthStore, workoutConfiguration: self.workoutConfig)

    //  this is the important bit 
    workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)!, predicate: nil)

    workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .heartRate)!, predicate: nil)
    workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!, predicate: nil)

})

You can adapt this for swimming and skiing.

There is currently no way to use workout builder to get distance on other types of outdoor activity, eg rowing.

--

Radar/Feedback details for if you want to Copy & Paste

Feedback No: FB5714600

Title: Support distance tracking for more exercise types

Body:

Currently live workout builder only includes support for tracking distance for the following types: - walking or running distance - skiing - swimming

I see two options. The best would be to allow something like:

workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .distancePaddling)!, predicate: nil)

Another option would be to allow workouts to be mutable so that the distance could be added retrospectively.

I'm happy to provide further input if required. Our users love the Apple Watch, but it is frustrating for them that they can't store the distance of their paddle workouts in Health.

lewis
  • 2,531
  • 2
  • 32
  • 56
  • Is there anything else needed outside of your code snippet to save Active Calories and Total Calories? – Austin Conlon Oct 28 '18 at 11:35
  • @AustinConlon not as far as I remember, they automatically collect some sensible defaults – lewis Oct 31 '18 at 16:12
  • @Lewis42 did you end up filing a radar for this? – GarySabo Feb 07 '20 at 14:28
  • @GarySabo yes. "Engineering has determined that your bug report is a duplicate of another issue and will be closed" although once it got imported into feedback it hasn't been closed. I added the radar text to my answer so you can copy and paste if you want to dupe it. – lewis Feb 08 '20 at 13:01
  • @Lewis42 thanks will do! If you were going to add distance to a mutable HKWorkout how would you collect distance samples? – GarySabo Feb 08 '20 at 16:34
  • @GarySabo you could collect them with a builder, I already do this, and then add the distance at the end. Not ideal, but better than no distance. – lewis Feb 10 '20 at 08:25
  • @Lewis42 oh I thought the point of the radar was that sports like rowing (my example is hockey) do not return distance samples when using a builder? – GarySabo Feb 10 '20 at 12:17
  • @garysabo well it's not the builder that is the problem I don't think, it's the actual activity. if you try and create say a rowing activity just in the health app it won't let you add a distance. – lewis Feb 11 '20 at 09:35