Last active
August 29, 2015 14:07
-
-
Save ccabanero/9aa030a57e62c243da40 to your computer and use it in GitHub Desktop.
Setting up a Health Store with Authorization from User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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