使用 HealthKit 检索 SleepAnalysis 后,我仍然收到此 Authorization Not Determined 错误
After using HealthKit to retrieve SleepAnalysis, I still get this Authorization Not Determined error
几天来我一直在参考这个错误,现在试图找出解决方案:
[query] Error activating query: Error Domain=com.apple.healthkit Code=5 "Authorization not determined" UserInfo={NSLocalizedDescription=Authorization not determined}
我一直在使用 HealthKit 成功检索睡眠数据,但现在我需要检索 activity 数据。我用这个函数设置了 HealthKit:
let typesToRead = Set([
HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!,
HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.appleStandHour)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.bloodType)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.height)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!,
HKObjectType.workoutType(),
HKObjectType.activitySummaryType()
])
self.healthStore.requestAuthorization(toShare: nil, read: typesToRead) { (sucess, error) -> Void in
if sucess == false {
NSLog("Error...")
}
}
然后我像这样创建查询:
let query = HKSampleQuery(sampleType: distanceType, predicate: nil, limit: 0, sortDescriptors: [startDateSort]) {
(sampleQuery, results, error) -> Void in
if let result = results {
for item in result {
if let sample = item as? HKQuantitySample {
self.workOutSamples.append(sample)
}
}
print(self.workOutSamples)
}
}
healthStore.execute(query)
我已将 Privacy - Health Update Usage Description
和 Privacy - Health Share Usage Description
添加到 info.plist 并且我的应用程序的功能包括成功打开 HealthKit。
已将 HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!
添加到 typesToRead。我的 post 没有显示我在创建 distanceType
时使用了错误的类型:quantityType
。
几天来我一直在参考这个错误,现在试图找出解决方案:
[query] Error activating query: Error Domain=com.apple.healthkit Code=5 "Authorization not determined" UserInfo={NSLocalizedDescription=Authorization not determined}
我一直在使用 HealthKit 成功检索睡眠数据,但现在我需要检索 activity 数据。我用这个函数设置了 HealthKit:
let typesToRead = Set([
HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!,
HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.appleStandHour)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.bloodType)!,
HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.height)!,
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!,
HKObjectType.workoutType(),
HKObjectType.activitySummaryType()
])
self.healthStore.requestAuthorization(toShare: nil, read: typesToRead) { (sucess, error) -> Void in
if sucess == false {
NSLog("Error...")
}
}
然后我像这样创建查询:
let query = HKSampleQuery(sampleType: distanceType, predicate: nil, limit: 0, sortDescriptors: [startDateSort]) {
(sampleQuery, results, error) -> Void in
if let result = results {
for item in result {
if let sample = item as? HKQuantitySample {
self.workOutSamples.append(sample)
}
}
print(self.workOutSamples)
}
}
healthStore.execute(query)
我已将 Privacy - Health Update Usage Description
和 Privacy - Health Share Usage Description
添加到 info.plist 并且我的应用程序的功能包括成功打开 HealthKit。
已将 HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!
添加到 typesToRead。我的 post 没有显示我在创建 distanceType
时使用了错误的类型:quantityType
。