Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Last active August 29, 2015 14:07
Show Gist options
  • Save ccabanero/9aa030a57e62c243da40 to your computer and use it in GitHub Desktop.
Save ccabanero/9aa030a57e62c243da40 to your computer and use it in GitHub Desktop.
Setting up a Health Store with Authorization from User
#import "ViewController.h"
#import <HealthKit/HealthKit.h>
@interface ViewController ()
@property (nonatomic, strong) HKHealthStore *healthStore;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeHealthStoreWithAuthorization];
}
- (void)initializeHealthStoreWithAuthorization {
if([HKHealthStore isHealthDataAvailable]) {
self.healthStore = [[HKHealthStore alloc] init];
NSSet *writeDataTypes = [self dataTypesToWrite];
NSSet *readDataTypes = [self dataTypesToRead];
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if(error) {
NSLog(@"%@", error);
} else {
NSLog(@"Success is %d", success);
}
}];
}
}
- (NSSet *)dataTypesToWrite {
HKQuantityType *bodyMass = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
HKQuantityType *distanceCycling = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
return [NSSet setWithObjects:bodyMass, distanceCycling, nil];
}
- (NSSet *)dataTypesToRead {
HKQuantityType *bodyMass = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
HKQuantityType *distanceCycling = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];
return [NSSet setWithObjects:bodyMass, distanceCycling, nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment