Skip to content

Instantly share code, notes, and snippets.

@max6363
Created May 8, 2018 19:13
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 max6363/4945c8172805c69c4459b149f5974009 to your computer and use it in GitHub Desktop.
Save max6363/4945c8172805c69c4459b149f5974009 to your computer and use it in GitHub Desktop.
/**
Write Steps
*/
- (void)writeSteps:(NSInteger)steps
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate
withFinishBlock:(void (^)(NSError *error))finishBlock
{
// write steps
// quantity type : steps
HKQuantityType *stepsQuantityType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
// generate count unit
HKUnit *unit = [HKUnit countUnit];
// generate quantity object with step count value
HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:steps];
// generate quantity sample object
HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:stepsQuantityType quantity:quantity startDate:startDate endDate:endDate];
// save sample object using health-store object
[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError * _Nullable error) {
NSLog(@"Saving steps to healthStore - success: %@", success ? @"YES" : @"NO");
dispatch_sync(dispatch_get_main_queue(), ^{
finishBlock(error);
});
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment