在实际尝试访问用户的dateofBirth和bioSex之前,我已经确定了授权。但这适用于模拟器,但不适用于iPhone和配对的手表。

    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可能会显示一个对话框,并在后台进程中为您提供结果。

在继续阅读生日和biologicalSex之前,您必须等待requestAuthorization的答案。

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

10-12 01:48