首页 > 解决方案 > WatchOS:每当启动 NRC 或 Runtastic 等第二个应用程序时,HKWorkoutSession 就会结束

问题描述

我正在测试 Apple 的 SpeedySloth 演示应用程序:https ://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/speedysloth_creating_a_workout

好吧,这到此结束,每当第二个应用程序启动时:

// 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()
                }
            }
        }
    }

当我在 Nike Running Club 中按下“开始”按钮时,直接使用 toState = .ended 调用委托。我假设一次只能进行一种锻炼,但是我可以将 Adidas Running 与 NRC 一起使用,因此,它一定是可能的。

标签: iosswiftobjective-ccocoawatchos

解决方案


HKWorkoutSession 的文档说明一次只能运行一个。

Apple Watch 一次运行一个健身课程。如果在您的锻炼运行时开始第二次锻炼,您的 HKWorkoutSessionDelegate 对象会收到 HKError.Code.errorAnotherWorkoutSessionStarted 错误,并且您的会话结束。

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

在 Apple Watch 推出之前,一些应用程序允许用户使用 CoreMotion API 测量跑步或步行会话。如果锻炼会话已经在运行,我怀疑您提到的其中一个应用程序可能会退回到此。


推荐阅读