HealthKit requestAuthorization dialog not appearing

We have been consistently unable to get the HealthKit requestAuthorization dialog to appear. This has been the case for two different phones with different OS versions and different installation methods (see below). The requestAuthorization code is standard boilerplate code, such as:


HKCharacteristicType *genderType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];
[self.healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithObjects: genderType, nil] completion:^(BOOL success, NSError *error) {
    NSLog(@"requestAuthorization completion block");
}];


We have tried a fresh app with just that code, tried different parameters for the requestAuthorization method call, and have tried resetting our systems in a number of different ways: app uninstall, resetting settings, erasing phone content and setting with existing partial backup; erasing phone content and settings with a full backup, erasing content and settings and starting phone as a new phone. Have tried multiple different readTypes and shareTypes. Each time requestAuthorization is called on a fresh app install, the following error code appears:


"Error occurred = Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo=0x7fa748534b00 {NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}"


However, the HealthKit entitlements are set in both Capabilities on xcode as well as Capabilities in the App ID on Apple Developer: Certificates, Identifiers, and Profiles.


- Questions: How do I fix this issue so the requestAuthorization dialog appears? What is a possible cause of phones getting into a state where they get this error consistently?


- Phones used: iPhone 6, iPhone 6 Plus

- OS Versions used: iOS 9.0 / 9.0.2 / 9.1 / 9.2

- Methods of loading app onto phone: iOS App Store, Watch App App Store, TestFlight, xcode

- Situations where this appears to not be broken: With our users' (non-development) phones installing via the App Store.

Did you include the HealthKit.framework under Linked Frameworks and Libraries?


I am using the following code to request authorization which works fine:


#import <HealthKit/HealthKit.h>


-(void)requestAccessDataTypes

{


self.healthStore = [[HKHealthStore alloc] init];


if ([HKHealthStore isHealthDataAvailable]) {

NSSet *writeDataTypes = [self dataTypesToWrite];

NSSet *readDataTypes = [self dataTypesToRead];

[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {

if (!success) {

NSLog(@"%@ - You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", [self class], error);

self.shouldSave = YES;

return;

} else {

self.shouldSave = success;

}

}];

}


}


- (NSSet *)dataTypesToRead {

HKQuantityType *heartRateType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

HKQuantityType *dietaryCalorieEnergyType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed];

HKQuantityType *activeEnergyBurnType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];

HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];

HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];

HKCharacteristicType *birthdayType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth];

HKCharacteristicType *biologicalSexType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];


HKObjectType *myWorkout = [HKObjectType workoutType];


return [NSSet setWithObjects:dietaryCalorieEnergyType, activeEnergyBurnType, heightType, weightType, birthdayType, biologicalSexType, myWorkout, heartRateType, nil];

}

Thanks for your response! I am using HealthKit.framework; the confusing part of this issue is that the HealthKit requestAuthorization dialog appears for our regular users, but only inconsistently on our develoment phones. So it's working in certain situations but not working in others.

How are you installing on the dev devices?


Might want to try ad-hoc, if you haven't already, which tends to better replicate store installs, I think.

HealthKit requestAuthorization dialog not appearing
 
 
Q