我目前正在尝试制作一个简单的计步器应用程序。但是,我似乎无法获得使用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时,它只是跳过了这些行。我的模拟器(和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)
}发布于 2018-05-02 10:36:19
也发生在我身上。它是通过升级我的手机到iOS 11.3.1来解决的。请注意,它在仿真器上运行平稳,即使有一个较低的iOS,所以我想它是一个xcode错误,升级到最新的xcode版本也可以解决这个问题。
https://stackoverflow.com/questions/49466366
复制相似问题