天天看点

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

{    

    //查询采样信息

    hkquantitytype *sampletype = [hkquantitytype quantitytypeforidentifier:hkquantitytypeidentifierstepcount];

    //nssortdescriptors用来告诉healthstore怎么样将结果排序。

    nssortdescriptor *start = [nssortdescriptor sortdescriptorwithkey:hksamplesortidentifierstartdate ascending:no];

    nssortdescriptor *end   = [nssortdescriptor sortdescriptorwithkey:hksamplesortidentifierenddate ascending:no];

    /*查询的基类是hkquery,这是一个抽象类,能够实现每一种查询目标,这里我们需要查询的步数是一个

     hksample类所以对应的查询类就是hksamplequery。

     下面的limit参数传1表示查询最近一条数据,查询多条数据只要设置limit的参数值就可以了

     */

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

    nspredicate *predicate = [hkquery predicateforsampleswithstartdate:startdate enddate:enddate options:hkqueryoptionstrictstartdate];

     // 从锚点为0处开始查询符合条件的所有记录

    hkanchoredobjectquery *q1 = [[hkanchoredobjectquery alloc] initwithtype:sampletype predicate:predicate anchor:hkanchoredobjectquerynoanchor limit:hkobjectquerynolimit completionhandler:^(hkanchoredobjectquery * _nonnull query, nsarray<__kindof hksample *> * _nullable results, nsuinteger newanchor, nserror * _nullable error) {

        double sum = 0;

//        nslog(@"results==%@", results);

        for (hkquantitysample *res in results)

            sum += [res.quantity doublevalueforunit:[hkunit countunit]];

        nslog(@"anchor%@查询步数=%@步", @(newanchor), @(sum));

    // 从锚点为0处开始查询符合条件的一条记录,查询结果得到的锚点是12160

    hkanchoredobjectquery *q2 = [[hkanchoredobjectquery alloc] initwithtype:sampletype predicate:predicate anchor:hkanchoredobjectquerynoanchor limit:1 completionhandler:^(hkanchoredobjectquery * _nonnull query, nsarray<__kindof hksample *> * _nullable results, nsuinteger newanchor, nserror * _nullable error) {

    // 从锚点12160处开始查询符合条件的一条记录,锚点起到分页查询的作用

    hkanchoredobjectquery *q3 = [[hkanchoredobjectquery alloc] initwithtype:sampletype predicate:predicate anchor:12160 limit:1 completionhandler:^(hkanchoredobjectquery * _nonnull query, nsarray<__kindof hksample *> * _nullable results, nsuinteger newanchor, nserror * _nullable error) {

            nslog(@"device name:%@", res.device.name);

            nslog(@"device manufacturer:%@", res.device.manufacturer);

            nslog(@"device hardwareversion:%@", res.device.hardwareversion);

            nslog(@"device firmwareversion:%@", res.device.firmwareversion);

            nslog(@"device softwareversion:%@", res.device.softwareversion);

            nslog(@"device localidentifier:%@", res.device.localidentifier);

            nslog(@"device udideviceidentifier:%@", res.device.udideviceidentifier);

            nslog(@"uuid:%@", res.uuid);

            hksource *source = nil;

            if ([uidevice currentdevice].systemversion.floatvalue >= 8)

            {

                source = res.sourcerevision.source;

                nslog(@"source version:%@", res.sourcerevision.version);

            } else

                source = res.source;

            }

            nslog(@"source:%@", source.name);

            nslog(@"source:%@", source.bundleidentifier);

            nslog(@"metadata:%@", res.metadata);

    //执行查询

    [_healthstore executequery:q1];

    [_healthstore executequery:q2];

    [_healthstore executequery:q3];