Apple Health Kit 错误域=com.apple.healthkit 代码=5 "Authorization not determined"

Apple Health Kit Error Domain=com.apple.healthkit Code=5 "Authorization not determined"

在实际尝试保存任何数据或对其进行任何操作之前,我已经确定了授权。查询时发生错误:

HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:foodType 
predicate:predicate 
limit:HKObjectQueryNoLimit 
sortDescriptors:nil 
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error){ ... }

是的,我已经阅读了 apple 文档,上面写着:

Attempts to save data before permission has been requested fail with a HKErrorAuthorizationNotDetermined error.

但我在尝试请求数据之前确实请求了权限(我看到了包含所有数据类型的列表(在代码中 - dataTypesToRead / Write))。 有人遇到过这类问题吗?

好的,找到问题了。在识别应用程序可以读取和写入哪些类型的数据时的数据类型中,我的睡眠分析类型错误。

错误:

HKCategoryType *sleepType = [HKSampleType categoryTypeForIdentifier:HKCategoryValueSleepAnalysisAsleep];

右:

HKCategoryType *sleepType = [HKSampleType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];