Created
December 1, 2016 17:45
-
-
Save akio46/0072a9e45b7192de8e6277e4ad7a50a7 to your computer and use it in GitHub Desktop.
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
+ (HealthKitManager *)sharedManager { | |
static dispatch_once_t pred = 0; | |
static HealthKitManager *instance = nil; | |
dispatch_once(&pred, ^{ | |
instance = [[HealthKitManager alloc] init]; | |
instance.healthStore = [[HKHealthStore alloc] init]; | |
instance.connected = [[NSUserDefaults standardUserDefaults] boolForKey:APPLE_HEALTHKIT_CONNECTION]; | |
}); | |
return instance; | |
} | |
- (void)requestAuthorization:(void(^)(BOOL success, NSError *error))callback { | |
if ([HKHealthStore isHealthDataAvailable] == NO) { | |
// If our device doesn't support HealthKit -> return. | |
return; | |
} | |
// Share body mass, height and body mass index | |
NSSet *shareObjectTypes = [NSSet setWithObjects: | |
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned], | |
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning], | |
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate], | |
[HKObjectType workoutType], | |
nil]; | |
// Read date of birth, biological sex and step count | |
NSSet *readObjectTypes = [NSSet setWithObjects: | |
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass], | |
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight], | |
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex], | |
nil]; | |
// Request access | |
[self.healthStore requestAuthorizationToShareTypes:shareObjectTypes | |
readTypes:readObjectTypes | |
completion:^(BOOL success, NSError *error) { | |
if(success == YES) { | |
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:APPLE_HEALTHKIT_CONNECTION]; | |
[[NSUserDefaults standardUserDefaults] synchronize]; | |
self.connected = YES; | |
} else { | |
NSLog(@"Apple HealthKit Connection Failed!"); | |
} | |
if (callback) | |
callback(success, error); | |
}]; | |
} | |
- (void)writeDistance:(NSNumber *)value startDate:(NSDate *)startDate endDate:(NSDate *)endDate{ | |
// Each quantity consists of a value and a unit. | |
HKUnit *unit = [HKUnit meterUnit]; | |
HKQuantity *distanceQuantity = [HKQuantity quantityWithUnit:unit doubleValue:value.floatValue]; | |
HKQuantityType *distanceType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]; | |
// For every sample, we need a sample type, quantity and a date. | |
HKQuantitySample *hrObj = [HKQuantitySample quantitySampleWithType:distanceType quantity:distanceQuantity startDate:startDate endDate:endDate]; | |
[self.healthStore saveObject:hrObj withCompletion:^(BOOL success, NSError *error) { | |
if (!success) { | |
NSLog(@"Error while saving value (%f) to Health Store: %@.", value.floatValue, error); | |
} | |
}]; | |
} | |
- (void)writeCalories:(NSNumber *)value startDate:(NSDate *)startDate endDate:(NSDate *)endDate{ | |
// Each quantity consists of a value and a unit. | |
HKUnit *unit = [HKUnit kilocalorieUnit]; | |
HKQuantity *caloriesQuantity = [HKQuantity quantityWithUnit:unit doubleValue:value.floatValue]; | |
HKQuantityType *caloriesType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned]; | |
// For every sample, we need a sample type, quantity and a date. | |
HKQuantitySample *hrObj = [HKQuantitySample quantitySampleWithType:caloriesType quantity:caloriesQuantity startDate:startDate endDate:endDate]; | |
[self.healthStore saveObject:hrObj withCompletion:^(BOOL success, NSError *error) { | |
if (!success) { | |
NSLog(@"Error while saving value (%f) to Health Store: %@.", value.floatValue, error); | |
} | |
}]; | |
} | |
- (void)writeWorkoutWithEnergyBurned:(NSNumber *)energyBurned distance:(NSNumber *)distance startDate:(NSDate *)startDate endDate:(NSDate *)endDate{ | |
HKUnit *unit_cal = [HKUnit kilocalorieUnit]; | |
HKQuantity *caloriesQuantity = [HKQuantity quantityWithUnit:unit_cal doubleValue:energyBurned.floatValue]; | |
HKUnit *unit_dis = [HKUnit meterUnit]; | |
HKQuantity *distanceQuantity = [HKQuantity quantityWithUnit:unit_dis doubleValue:distance.floatValue]; | |
HKWorkout *workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeRowing | |
startDate:startDate | |
endDate:endDate | |
duration:[endDate timeIntervalSinceDate:startDate] | |
totalEnergyBurned:caloriesQuantity | |
totalDistance:distanceQuantity | |
metadata:nil]; | |
[self.healthStore saveObject:workout withCompletion:^(BOOL success, NSError *error) { | |
if (!success) { | |
NSLog(@"Error while saving value (%f), value (%f) to Health Store: %@.", distance.floatValue, energyBurned.floatValue, error); | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment