- (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));
// 间隔一小时统计一次
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) {
nslog(@"统计init出错 %@", error);
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) {
nslog(@"统计update出错 %@", error);
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];