- (void)queryStepCount
{
if (![HKHealthStore isHealthDataAvailable])
{
NSLog(@"设备不支持healthKit"); return;
}
_healthStore = [[HKHealthStore alloc] init];
HKObjectType *type1 = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; // 步数
NSSet *set = [NSSet setWithObjects:type1, nil]; // 读集合
__weak typeof (&*self) weakSelf = self;
[_healthStore requestAuthorizationToShareTypes:nil readTypes:set completion:^(BOOL success, NSError * _Nullable error) {
if (success)
{
[weakSelf readStepCount];
} else
{
NSLog(@"healthkit不允许读写");
}
}];
}
//查询数据
- (void)readStepCount
{
// 统计分析
sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateCom = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSDate *startDate, *endDate;
endDate = [calendar dateFromComponents:dateCom];
[dateCom setHour:0];
[dateCom setMinute:0];
[dateCom setSecond:0];
startDate = [calendar dateFromComponents:dateCom];
predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];
NSUInteger op = HKStatisticsOptionCumulativeSum;
HKStatisticsQuery *q1 = [[HKStatisticsQuery alloc] initWithQuantityType:sampleType quantitySamplePredicate:predicate options:op completionHandler:^(HKStatisticsQuery * _Nonnull query, HKStatistics * _Nullable result, NSError * _Nullable error) {
NSLog(@"\n\n");
if (error)
{
NSLog(@"统计出错 %@", error);
return;
}
double sum1 = [result.averageQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum2 = [result.minimumQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum3 = [result.maximumQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum4 = [result.sumQuantity doubleValueForUnit:[HKUnit countUnit]];
NSLog(@"统计平均步数=%@步", @(sum1));
NSLog(@"统计最小步数=%@步", @(sum2));
NSLog(@"统计最大步数=%@步", @(sum3));
NSLog(@"统计步数=%@步", @(sum4));
NSLog(@"\n\n");
}];
// 间隔一小时统计一次
NSDateComponents *hComponents = [calendar components:NSCalendarUnitHour fromDate:[NSDate date]];
[hComponents setHour:1];
HKStatisticsCollectionQuery *q2 =[[HKStatisticsCollectionQuery alloc] initWithQuantityType:sampleType quantitySamplePredicate:predicate options:op anchorDate:startDate intervalComponents:hComponents];
q2.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection * __nullable result, NSError * __nullable error) {
if (error)
{
NSLog(@"统计init出错 %@", error);
return;
}
for (HKStatistics *s in result.statistics)
{
double sum1 = [s.averageQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum2 = [s.minimumQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum3 = [s.maximumQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum4 = [s.sumQuantity doubleValueForUnit:[HKUnit countUnit]];
NSLog(@"init统计平均步数=%@步", @(sum1));
NSLog(@"init统计最小步数=%@步", @(sum2));
NSLog(@"init统计最大步数=%@步", @(sum3));
NSLog(@"init统计步数=%@步", @(sum4));
}
};
q2.statisticsUpdateHandler = ^(HKStatisticsCollectionQuery *query, HKStatistics * __nullable statistics, HKStatisticsCollection * __nullable collection, NSError * __nullable error) {
if (error)
{
NSLog(@"统计update出错 %@", error);
return;
}
for (HKStatistics *result in collection.statistics)
{
double sum1 = [result.averageQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum2 = [result.minimumQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum3 = [result.maximumQuantity doubleValueForUnit:[HKUnit countUnit]];
double sum4 = [result.sumQuantity doubleValueForUnit:[HKUnit countUnit]];
NSLog(@"update统计平均步数=%@步", @(sum1));
NSLog(@"update统计最小步数=%@步", @(sum2));
NSLog(@"update统计最大步数=%@步", @(sum3));
NSLog(@"update统计步数=%@步", @(sum4));
}
};
//执行查询
[_healthStore executeQuery:q1];
[_healthStore executeQuery:q2];
}