I'm currently having an issue tracking active calories and the total duration of workouts on the Apple Watch. Sometimes it is correct and sometimes they are relatively low. Workout generally lasts 50~70 mins, but it logs only 16 mins, 21 mins, or some random number on Apple Watch and in Apple Health. Same for calories.
iPhone workout app:
func startWatchApp(...) {
getActiveWCSession { wcSession in
if wcSession.activationState == .activated && wcSession.isWatchAppInstalled {
let configuration = HKWorkoutConfiguration()
configuration.activityType = .highIntensityIntervalTraining
configuration.locationType = .indoor
self.healthStore.startWatchApp(with: configuration, completion: { (success, error) in
print("⌚️ started watch app with error: \(error?.localizedDescription ?? "No error")")
})
} else{
print("⌚️ watch not active or not installed")
}
}
}
Watch App:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
func handle(_ workoutConfiguration: HKWorkoutConfiguration) {
WorkoutManager.shared.startWorkout(with: workoutConfiguration)
}
}
class WorkoutManager: NSObject, ObservableObject {
func startWorkout(with configuration: HKWorkoutConfiguration) {
do {
let session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
setupWorkoutWith(session: session)
} catch {
print("Error initializing HealthKit workout session. \(error.localizedDescription)")
return
}
}
func setupWorkoutWith(session: HKWorkoutSession) {
self.session = session
let configuration = session.workoutConfiguration
builder = session.associatedWorkoutBuilder()
builder?.dataSource = HKLiveWorkoutDataSource(
healthStore: healthStore,
workoutConfiguration: configuration
)
session.delegate = self
builder?.delegate = self
builder?.shouldCollectWorkoutEvents = true
// Start the workout session and begin data collection
// if session already started(recovered)
let startDate = session.startDate ?? Date()
if session.startDate == nil {
session.startActivity(with: startDate)
}
builder?.beginCollection(withStart: startDate, completion: { success, error in
if let error = error {
print("Workout builder begin collection error: \(error.localizedDescription)")
}
})
}
func endSession() {
session?.end()
builder?.endCollection(withEnd: Date(), completion: { success, error in
self.builder?.finishWorkout(completion: { workout, error in
DispatchQueue.main.async {
if let error = error {
print("save workout error: \(error.localizedDescription)")
} else {
self.workout = workout
}
}
})
})
}
Is there anything I missed or should consider when building workout apps?