Home > Mobile >  WatchOS: HKWorkoutSession ends whenever a second app like NRC or Runtastic is started
WatchOS: HKWorkoutSession ends whenever a second app like NRC or Runtastic is started

Time:09-18

I am testing the SpeedySloth demo app from Apple: https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/speedysloth_creating_a_workout

Well, this ends here, whenever a second app starts:

// MARK: - HKWorkoutSessionDelegate
extension WorkoutManager: HKWorkoutSessionDelegate {
    func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState,
                        from fromState: HKWorkoutSessionState, date: Date) {
        // Wait for the session to transition states before ending the builder.
        /// - Tag: SaveWorkout
        if toState == .ended {
            print("The workout has now ended.")
            builder.endCollection(withEnd: Date()) { (success, error) in
                self.builder.finishWorkout { (workout, error) in
                    // Optionally display a workout summary to the user.
                    self.resetWorkout()
                }
            }
        }
    }

The delegate is directly called with toState = .ended when I press the "Start" Button in Nike Running Club. I assume, that there is only one workout possible at one time, BUT I can use Adidas Running along with NRC, so, it must be somehow possible.

CodePudding user response:

The documentation for HKWorkoutSession states that only one can be run at a time.

Apple Watch runs one workout session at a time. If a second workout starts while your workout is running, your HKWorkoutSessionDelegate object receives an HKError.Code.errorAnotherWorkoutSessionStarted error, and your session ends.

See https://developer.apple.com/documentation/healthkit/hkworkoutsession

Before the Apple Watch came out several Apps allowed users to measure a run or walk session by using the CoreMotion APIs. I suspect one of the Apps you mention may fall back to this if a Workout Session is already running.

  • Related