ios - 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 一起使用,因此,它一定是可能的。
解决方案
HKWorkoutSession 的文档说明一次只能运行一个。
Apple Watch 一次运行一个健身课程。如果在您的锻炼运行时开始第二次锻炼,您的 HKWorkoutSessionDelegate 对象会收到 HKError.Code.errorAnotherWorkoutSessionStarted 错误,并且您的会话结束。
见https://developer.apple.com/documentation/healthkit/hkworkoutsession
在 Apple Watch 推出之前,一些应用程序允许用户使用 CoreMotion API 测量跑步或步行会话。如果锻炼会话已经在运行,我怀疑您提到的其中一个应用程序可能会退回到此。
推荐阅读
- python - 元组元素在列表中的位置
- sql - SQLite / DB 浏览器中的 SQL 查询不返回任何值
- azure-devops - “自动链接此版本中的新作品”和保留政策
- javascript - JS:创建工厂函数而不是类时如何创建原型?
- jasper-reports - 如何使用 JasperReport 6.7.0 在 lineChart 上自定义轴?
- javascript - 我无法在 vue projet 中使用原始 node.js util 模块
- c# - SendGrid V3 使用用户名和密码连接
- python - AWS DLAMI 中的 Pandas np.array 列
- swift - 从多个 UICollectionVIews 内的 UICollectionViewCell 推送新的 UICollectionViewController
- android - 传递自定义对象的数组列表以从 android 响应本机端