20

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];
Sean
  • 1,045
  • 7
  • 22

2 Answers2

24

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

jrushing
  • 878
  • 1
  • 8
  • 11
  • 3
    Was this documented anywhere? I understand the data itself is sensitive, but the permission itself shouldn't be. – Sean Aug 27 '14 at 13:26
  • 1
    It is explained at the end of the HealthKit talk – jrushing Aug 27 '14 at 16:35
  • 1
    The API documentation (dated Sep 8, 2014) has come out backing up jrushing's statement : "To help prevent possible leaks of sensitive health information, your app cannot determine whether or not a user has granted permission to read data." https://developer.apple.com/library/prerelease/iOS/documentation/HealthKit/Reference/HKHealthStore_Class/index.html – RobLabs Oct 03 '14 at 16:56
  • 9
    I don't understand how it's going to help. If you can't get the data it can be either the data is not there or it's disabled - what's the difference? I just have to tell the user I can't get such data so either give me the permission or don't use my app. – superarts.org Nov 03 '14 at 05:00
  • 2
    @superarts.org A user's preferences might indicate health information by proxy. For example, you could use machine learning on responses from users with known medical conditions, and then input the preferences of an unknown user and match them to whichever condition is closest. – dmur Mar 07 '16 at 23:49
  • Ok, so I can't understand if a user denied the permission to read some data, but can I, at least, know if I haven't ask for that permission at all? If the permission is UNDEFINED (for a read type) – superpuccio Oct 18 '16 at 15:24
1
        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;
                }


            }

        }];

    }
Max Gribov
  • 388
  • 2
  • 7
  • 1
    Sadly this trick no longer works in iOS 10. It is returning a correct HKUnit instead of error, even if permission is denied for the given quantity type. – sobri Oct 14 '16 at 09:21
  • This works only if authorization was never asked for the vital that is being read. But if the authorization to read was asked, irrespective whether it was allowed or not, this error code 5 won't come. – Sayalee Pote Jul 09 '19 at 09:50