HealthKit HKAuthorizationStatus for reading data

五迷三道 提交于 2020-01-01 04:08:05

问题


I'm using HealthKit to read certain types of information. 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. 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];

回答1:


For privacy reasons, you are unable to view your application's read authorization status for a particular type.




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


            }

        }];

    }


来源:https://stackoverflow.com/questions/25512320/healthkit-hkauthorizationstatus-for-reading-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!