I am calling a function and have been trying to figure out how I can update my app to reflect the total steps for the day. Currently, the code I have only gives me the most recent quantity of steps that has been recorded. I am having trouble understanding how to use the HKStatisticsQuery
but this is the function I have at the moment that is giving me the most recent results.
-(void)updateStepsLabel{
// Query to get the user's latest steps, if it exists.
HKQuantityType *stepsType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
[_healthStore aapl_mostRecentQuantitySampleOfType:stepsType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"Either an error occured fetching the user's steps information or none has been stored yet. In your app, try to handle this gracefully.");
dispatch_async(dispatch_get_main_queue(), ^{
self.todaysStepValueLabel.text = NSLocalizedString(@"Not available", nil);
});
}
else {
// Determine the steps in the required unit.
HKUnit *stepUnit = [HKUnit countUnit];
double usersWeight = [mostRecentQuantity doubleValueForUnit:stepUnit];
// Update the user interface.
dispatch_async(dispatch_get_main_queue(), ^{
self.todaysStepValueLabel.text = [NSNumberFormatter localizedStringFromNumber:@(usersWeight) numberStyle:NSNumberFormatterNoStyle];
self.todaysStepValueLabel.textColor = [UIColor blackColor];
self.todaysStepValueLabel.font = [UIFont systemFontOfSize:25];
});
}
}];
}