在 watchOS 2 的 beta 2 中,模拟器和我的设备都可以请求和使用 HealthKit 数据。
但是现在在 beta 3 上,似乎发生了一些变化。模拟器仍然可以像我预期的那样请求 HealthKit 访问,但 Apple Watch 似乎从来没有要求它,我收到以下错误:
发生错误 = 错误域 = com.apple.healthkit 代码 = 4“缺少 com.apple.developer.healthkit 权利。” UserInfo=0x7fa748534b00 {NSLocalizedDescription=缺少 com.apple.developer.healthkit 权利。
还有人看到这个吗?它是否可以在模拟器上工作,但不能在真正的 Apple Watch 上工作,这是我的错?很难找到答案,因为目前构建 Apple Watch 需要很长时间。
接口控制器:
let healthKitManager = HealthKitManager()
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if healthKitManager.checkForHealthKitCapabilities() {
print("HealthKit is available")
// If device has HealthKit capabilities request access
healthKitManager.requestHealthKitAccess()
} else {
print("HealthKit not available\n")
return
}
}
健康套件管理器:
func checkForHealthKitCapabilities() -> Bool {
return HKHealthStore.isHealthDataAvailable()
}
func requestHealthKitAccess() {
let typesToShare = Set([
self.heartRateType,
])
let typesToRead = Set([
self.heartRateType,
])
self.healthStore.requestAuthorizationToShareTypes(typesToShare, readTypes: typesToRead) {
success, error in
if error != nil {
print("RequestHealthKit \(error)")
} else {
print("We got access")
}
}
}