Skip to content

Instantly share code, notes, and snippets.

@anka
Created January 4, 2015 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anka/d113aef2654d64460e84 to your computer and use it in GitHub Desktop.
Save anka/d113aef2654d64460e84 to your computer and use it in GitHub Desktop.
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