天天看點

HealthKit之實戰統計讀取

- (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];