该错误通常是因用户没有给予应用的健康数据权限导致。解决方法可以参考以下代码:
let healthStore = HKHealthStore() let typesToShare = Set([HKObjectType.workoutType(), HKSeriesType.workoutRoute(), HKObjectType.quantityType(forIdentifier: .heartRate)!, HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!])
let typesToRead = Set([HKObjectType.workoutType(), HKSeriesType.workoutRoute(), HKObjectType.quantityType(forIdentifier: .heartRate)!, HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!])
healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead) { success, error in if !success { print("错误:(error!.localizedDescription)") } }
以上示例代码请求共享及读取健康数据,如果用户没有给予权限,则会返回错误信息并输出错误描述。在调用requestAuthorization()方法前确保已在info.plist中配置相关隐私权限。