Apple Health Kit错误域= com.apple.healthkit代码= 5“授权未确定”

时间:2016-11-03 07:44:24

标签: ios swift watch-os-2 health-kit

在实际尝试访问用户的dateofBirth和biologicalSex之前,我确定了授权。但它适用于模拟器。但不适用于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)")
    }

1 个答案:

答案 0 :(得分:1)

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

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

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