ios – The way to upload “Estimated Time in Every Center Price Zone” data to my customized HKWorkout when considered in Apple Health app?


I’m currently developing a fitness app for watchOS that lets a user to manually set a desired heart rate target zone (enter numbers representing the lower and upper boundaries) and start a workout (right now it’s only “Other” type). After that my app monitors user’s heart rate and alerts them when they’re out of zone.

When user ends the workout, the info about this workout appears on “Fitness” iOS app, and user can see the workout data like Workout Time, Active and Total Calories, Avg. Heart Rate. Also user can see Heart Rate chart with info how their heart rate was changing during a workout (see the Figure 1).

Now to the question.

When user clicks “Show More” button above the Heart Rate chart, they can see the same Heart Rate chart and another one, with Post-Workout Heart Rate (see the Figure 2).

But there is no “Estimated time in each heart rate zone” as one can see in the workout’s details that were recorded from Apple’s workout (watchOS “Workout” app, for a workout of “Other” type as well), please see the Figure 3.

The question is: is it possible to add “Estimated time in each heart rate zone” to workout recorded via my third-party app so it would look like on the Figure 3 in “Fitness” iOS app, and if it’s possible, what steps should I undertake to implement this?

How I set up the HKHealthStore to use it for and HKWorkoutConfiguration, HKWorkoutSession, HKLiveWorkoutBuilder:

    var hkObject: HKHealthStore?
    
    let workoutConfig = HKWorkoutConfiguration()
    var workoutSession: HKWorkoutSession? = nil
    var workoutBuilder: HKLiveWorkoutBuilder? = nil
    
    init() {
        hkObject = HKHealthStore()
        hkObject?.requestAuthorization(toShare: [HKQuantityType.workoutType()], read: [HKQuantityType(.heartRate), HKQuantityType(.activeEnergyBurned), HKQuantityType(.basalEnergyBurned)]) {(success, error) in
            if success {
                print("Authorization succeeded")
            } else {
                print("Authorization failed")
            }
        }
        
        workoutConfig.activityType = .other
        do {
            workoutSession = try HKWorkoutSession(healthStore: hkObject!, configuration: workoutConfig)
            workoutBuilder = workoutSession?.associatedWorkoutBuilder()
            workoutBuilder?.dataSource = HKLiveWorkoutDataSource(healthStore: hkObject!, workoutConfiguration: workoutConfig)
            print("Workout session and builder have been created successfully")
        } catch let error {
            ......
        }
        
    }
        

What I can see in Fitness app now (Figures 1-2) and what I would like to see (Figure 3)

enter image description here

Figure 1, general info about the workout from my app in “Fitness” app

enter image description here

Figure 2, detailed info about Heart Rate during the workout from my app in “Fitness” app

enter image description here
Figure 3, what I would like to have: detailed info about Heart Rate with heart rate zones during the workout recorded by watchOS “Workout” app in “Fitness” app

Thanks in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *