Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
iOS HKHealthStore request authorization
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
// Share body mass, height and body mass index
NSSet *shareObjectTypes = [NSSet setWithObjects:
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex],
nil];
// Read date of birth, biological sex and step count
NSSet *readObjectTypes = [NSSet setWithObjects:
[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth],
[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
nil];
// Request access
[healthStore requestAuthorizationToShareTypes:shareObjectTypes
readTypes:readObjectTypes
completion:^(BOOL success, NSError *error) {
if(success == YES)
{
// ...
}
else
{
// Determine if it was an error or if the
// user just canceld the authorization request
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment