HealthKit HKAuthorizationStatus 用于读取数据

Posted

技术标签:

【中文标题】HealthKit HKAuthorizationStatus 用于读取数据【英文标题】:HealthKit HKAuthorizationStatus for reading data 【发布时间】:2014-08-26 18:02:39 【问题描述】:

我正在使用 HealthKit 来读取某些类型的信息。我不是特别要求写功能。当试图检测用户是否允许读取某种健康类型时,就会出现问题。

我相信这样做的预期方法是使用 HKHealthStore 的 authorizationStatusForType 方法,但这只会返回被拒绝或未知。它只返回 write 类型的授权。有没有人找到一种方法来使用这种方法来阅读或解决其他问题?

HKQuantityType *stepsType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKAuthorizationStatus status = [self.healthStore authorizationStatusForType:stepsType];

【问题讨论】:

【参考方案1】:

出于隐私原因,您无法查看应用程序对特定类型的读取授权状态。

【讨论】:

这是否记录在任何地方?我知道数据本身很敏感,但权限本身不应该。 在 HealthKit 演讲的最后有解释 API 文档(日期为 2014 年 9 月 8 日)出来支持 jrushing 的声明:“为了帮助防止敏感健康信息可能泄露,您的应用无法确定用户是否已授予读取数据。” developer.apple.com/library/prerelease/ios/documentation/… 我不明白它会有什么帮助。如果您无法获取数据,则可能是数据不存在或已禁用 - 有什么区别?我只需要告诉用户我无法获取此类数据,因此要么授予我权限,要么不要使用我的应用。 @superarts.org 用户的偏好可能通过代理指示健康信息。例如,您可以对已知医疗状况的用户的反应使用机器学习,然后输入未知用户的偏好并将其与最接近的状况相匹配。【参考方案2】:
        NSArray *quantityTypesUsedInApp = @[HKQuantityTypeIdentifierBodyMass,
                                             HKQuantityTypeIdentifierHeight,
                                             HKQuantityTypeIdentifierBodyMassIndex,
                                             HKQuantityTypeIdentifierBodyFatPercentage,
                                             HKQuantityTypeIdentifierLeanBodyMass];

    for (NSString *identifier in quantityTypesUsedInApp) 

        HKQuantityType *sampleType = [HKQuantityType quantityTypeForIdentifier:identifier];
        NSSet *requestSampleUnit = [NSSet setWithObject:sampleType];

        [self.healthKitStore preferredUnitsForQuantityTypes:requestSampleUnit completion:^(NSDictionary *preferredUnits, NSError *error) 

            if (!error) 

                HKUnit *unit = [preferredUnits objectForKey:sampleType];
                NSLog(@"%@ : %@", sampleType.identifier, unit.unitString);
                //sampleType enabled for read

             else 

                switch (error.code) 
                    case 5:

                        NSLog(@"%@ access denied", sampleType.identifier);
                       //sampleType denied for read
                        break;

                    default:
                        NSLog(@"request preffered quantity types error: %@", error);
                        break;
                


            

        ];

    

【讨论】:

遗憾的是,这个技巧在 iOS 10 中不再有效。即使给定数量类型的权限被拒绝,它也会返回正确的 HKUnit 而不是错误。 这仅适用于从未要求对正在读取的重要信息进行授权的情况。但是如果询问授权阅读,不管是否允许,都不会出现这个错误码5。

以上是关于HealthKit HKAuthorizationStatus 用于读取数据的主要内容,如果未能解决你的问题,请参考以下文章

HealthKit、核心数据和 CloudKit

Meteor 中的 Healthkit 插件

Meteor 中的 Healthkit 插件

HealthKit - 显示睡眠

HealthKit - 显示睡眠

从 HealthKit 存储数据