简体   繁体   English

HealthKit HKAuthorizationStatus用于读取数据

[英]HealthKit HKAuthorizationStatus for reading data

I'm using HealthKit to read certain types of information. 我正在使用HealthKit来读取某些类型的信息。 I'm specifically not asking for write functionality. 我特别不要求写功能。 The problem comes in when trying to detect if the user has allowed a certain Health type to be read. 当尝试检测用户是否允许读取某个健康类型时,会出现此问题。

I believe the intended way to do this is using an HKHealthStore's authorizationStatusForType method, but this is only returned denied or unknown. 我相信这样做的目的是使用HKHealthStore的authorizationStatusForType方法,但这只是被拒绝或未知。 It is only returning authorized for write types. 它只返回授权写入类型。 Has anyone found a way to use this method for reading or another work around? 有没有人找到一种方法来使用这种方法阅读或另一种​​解决方法?

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

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

        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;
                }


            }

        }];

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM