Reputation: 91
I hava determined authorization before actually trying to access the user's dateofBirth and biologicalSex. But it's work on simulator.but not on iphone and paired 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)")
}
Upvotes: 2
Views: 3986
Reputation: 6869
This is a concurrency problem, not a HealthKit problem.
requestAuthorization
may display a dialog and gives you the result in a background process.
You have to wait for the answer of requestAuthorization
before you continue reading birthday and biologicalSex.
Upvotes: 2