简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM