watchOS 5:HKLiveWorkoutBuilder,HKWorkoutRouteBuilder和锻炼距离

时间:2018-09-10 21:24:01

标签: watchkit health-kit watch-os-5

我正在使用HKLiveWorkoutBuilderHKWorkoutRouteBuilder在Apple Watch上跟踪锻炼。

致电时:

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

}];

返回的workout对象不包括用户行进的距离:

(lldb) po workout.totalDistance
nil

但是路线已正确保存。

鉴于.totalDistance是只读的,我应该如何在watchOS 5中设置锻炼的距离?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

答案有两部分。

要使其适用于步行或跑步,您可以使用以下代码:

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)

})

您可以将其调整为游泳和滑雪。

当前无法使用健身工具来获取其他类型的户外活动的距离,例如划船。

我将为此提起诉讼。