在实际尝试访问用户的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)")
}
答案 0 :(得分:1)
这是一个并发问题,而不是HealthKit问题。
requestAuthorization
可能会显示一个对话框,并在后台进程中为您提供结果。
在继续阅读生日和biologicalSex之前,您必须等待requestAuthorization
的答案。