gpt4 book ai didi

swift - 请求 HealthKit 授权未执行。 (HKHealthStore().requestAuthorization)

转载 作者:行者123 更新时间:2023-11-28 14:55:07 32 4
gpt4 key购买 nike

我目前正在尝试制作一个简单的计步器应用程序。但是,我似乎无法获得使用 HealthKit 的授权。

我有这段代码试图获得许可并在之后使用回调来处理结果。

func authorizeHealthKit(withCallback: @escaping (Bool, Error?) -> ())
{
// 3. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable() {
let error = NSError(domain: "health_tracker", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available in this Device"])
print(error)
}
else {
// 4. Request HealthKit authorization
self._healthKitStore.requestAuthorization(toShare: _healthKitTypesToWrite, read: _healthKitTypesToRead) { (success, error) -> Void in
self.isAuthorized = success
withCallback(success, error)
}
}
}

不过,出于某种原因,iOS 似乎完全跳过了那条线。在回调函数(authorizeHealthKit 调用)上设置断点时,没有任何反应,也不会停止。使用 Step Over 时,它只是跳过这些行。我的模拟器(和 irl iPhone)的屏幕是白色的,没有弹出对话。

知道为什么会发生这种情况吗?我做错了什么吗?


附加信息:

在同一个类中 (HealthKitManager.swift)

let _healthKitStore: HKHealthStore = HKHealthStore()
var isAuthorized: Bool = false
let _healthKitTypesToWrite: Set<HKSampleType> = [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
]

let _healthKitTypesToRead: Set<HKSampleType> = [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
]

在另一个类中,调用上面的方法:

let _healthKitManager = HealthKitManager()
(...)
if !_healthKitManager.isAuthorized {
_healthKitManager.authorizeHealthKit(withCallback: onAuthenticationResult)
}
(...)
func onAuthenticationResult(success: Bool, error: Error?) {
print("Authentication result: ", success.description)
}

最佳答案

我也遇到过。通过将我的手机升级到 iOS 11.3.1 解决了这个问题。请注意,即使在较低的 iOS 上它也能在模拟器上顺利运行,所以我猜这是一个 xcode 错误,升级到最新的 xcode 版本也可以解决它...

关于swift - 请求 HealthKit 授权未执行。 (HKHealthStore().requestAuthorization),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49466366/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com