14

I am using HealthKit in my app. I am getting the Permission from the user for accessing the HealthKit Data. After the Authorisation, if I check for authorised status for a particular HealthKit Object type, it always returns that the access is denied. (1 is the enum integer Value).

Here is my code

// Steps

if ([self.healthStore authorizationStatusForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]] == HKAuthorizationStatusSharingAuthorized) {
    [self accessStepsFrom:fromDate to:toDate];
}

//Sleep
if ([self.healthStore authorizationStatusForType:[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis]] == HKAuthorizationStatusSharingAuthorized) {
    [self accessSleepFrom:fromDate to:toDate];
}

//DOB
if ([self.healthStore authorizationStatusForType:[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]] == HKAuthorizationStatusSharingAuthorized) {
    [self accessDOB];
}

The method [self.healthStore authorizationStatusForType:[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]] always throws me 1. Need help on this ?

iranjith4
  • 378
  • 2
  • 13
  • I'm seeing the same behavior. I confirmed sharing is enabled in the Health app for my app for height, but the response from authorizationStatusForType: is still HKAuthorizationStatusSharingDenied. This is in the simulator for me. Have you tried on a device? – Mattio Mar 17 '15 at 14:44
  • Yeah. Its same on the Device. Looking for a fix in Next update. – iranjith4 Mar 17 '15 at 14:47
  • @iranjith4 Are you able to find the solution for this problem ?. I am facing the same issue getting denied every time even permission granted, is there any work around of it ? – Prashant Tukadiya Apr 03 '19 at 07:03
  • 1
    @PrashantTukadiya No way of getting the status as far I know. Apple deliberately denies. More info at https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKHealthStore_Class/index.html#//apple_ref/occ/instm/HKHealthStore/authorizationStatusForType: – iranjith4 Apr 13 '19 at 12:05

2 Answers2

44

The authorization status for an HKObjectType does not reflect whether your application has authorization to read samples of those types. It only indicates whether you have requested authorization at all and whether your app is authorized to write samples of those types. So if your app requests authorization to read step count samples but not write them, and the user grants read authorization, then the authorization status for HKQuantityTypeIdentifierStepCount will be HKAuthorizationStatusSharingDenied.

The following is from the HealthKit framework reference and explains why your app may not query whether it has read access:

To help prevent possible leaks of sensitive health information, your app cannot determine whether or not a user has granted permission to read data. If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store. If your app is given share permission but not read permission, you see only the data that your app has written to the store. Data from other sources remains hidden.

Allan
  • 7,039
  • 1
  • 16
  • 26
-1
NSArray *readTypes = @[[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];

[self.healthStore requestAuthorizationToShareTypes:nil
                                         readTypes:[NSSet setWithArray:readTypes] completion:nil];
Nil Rathod
  • 450
  • 5
  • 21