ios - Apple Health Kit 错误域=com.apple.healthkit 代码=5 “Authorization not determined”

标签 ios swift watchos-2 healthkit

在实际尝试访问用户的出生日期和生物性别之前,我已经确定了授权。但它可以在模拟器上运行,但不能在 iPhone 和配对的 watch 上运行。

    let birthdayType = HKQuantityType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)
    let biologicalSexType = HKQuantityType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex)
    let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)
    let readDataTypes: Set<HKObjectType> =  [quantityType!, birthdayType!, biologicalSexType!]
    guard HKHealthStore.isHealthDataAvailable() == true else {
        label.setText("not available")
        return
    }

    let readDataTypes: Set<HKObjectType> = self.dataTypesToRead()

    healthStore.requestAuthorization(toShare: nil, read: readDataTypes) { (success, error) -> Void in
        if success == false {
            self.displayNotAllowed()
        }
    }

    var birthDay: Date! = nil
    do {
       birthDay = try self.healthStore.dateOfBirth()
    } catch let error as NSError{
        print("Either an error occured fetching the user's age information or none has been stored yet. \(error)")
        return -1
    }
    var biologicalSex:HKBiologicalSexObject! = nil
    do {
        try biologicalSex = self.healthStore.biologicalSex()
    }catch let error as NSError{
        print("Failed to read the biologicalSex! \(error)")
    }

最佳答案

这是一个并发问题,而不是 HealthKit 问题。

requestAuthorization 可能会显示一个对话框并在后台进程中为您提供结果。

您必须等待requestAuthorization的答复才能继续读取生日和biologicalSex。

关于ios - Apple Health Kit 错误域=com.apple.healthkit 代码=5 “Authorization not determined”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396194/

相关文章:

iphone - 加载 NSMutableArray 到 UITableView 滚动时崩溃

swift - 如何在 Apple Watch 上绘制自定义图形?

ios - 在 UICollectionView 单元格中隐藏按钮

ios - Jenkins iOS 的持续集成。我可以在每次执行构建作业时运行 pod install

swift - 快速更改 UINavigationBar 中 UIBarButtonItem 的宽度

ios - 如何快速切换到最后一页上的不同 View Controller ?

ios - 使用 AVAudioRecorder 录制语音

watchkit - 如何在后台模式下从 HealthKit 访问心率和 body 事件数据?

swift - 从 WatchKit 2 中选定的选择器行获取值

ios - 根据架构在 Objective-C 中进行条件导入