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) {

        double sum = 0;

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

        for (HKQuantitySample *res in results)

        {

            sum += [res.quantity doubleValueForUnit:[HKUnit countUnit]];

        }

        NSLog(@"Anchor%@查询步数=%@", @(newAnchor), @(sum));

    }];

    // 从锚点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) {

        double sum = 0;

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

        for (HKQuantitySample *res in results)

        {

            sum += [res.quantity doubleValueForUnit:[HKUnit countUnit]];

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

        }

        NSLog(@"Anchor%@查询步数=%@", @(newAnchor), @(sum));

    }];

        

    //执行查询

    [_healthStore executeQuery:q1];

    [_healthStore executeQuery:q2];

    [_healthStore executeQuery:q3];

}


目录
相关文章
|
4月前
在智能媒体服务中,如果你想要让两个字幕重叠,你可以尝试以下方法
在智能媒体服务中,如果你想要让两个字幕重叠,你可以尝试以下方法【1月更文挑战第18天】【1月更文挑战第90篇】
66 6
|
1月前
|
定位技术 索引
osgEarth使用笔记3——加载倾斜摄影数据
osgEarth使用笔记3——加载倾斜摄影数据
44 1
|
3月前
|
Linux iOS开发 MacOS
【随手记】maplotlib.use函数设置图像的呈现方式
【随手记】maplotlib.use函数设置图像的呈现方式
35 0
|
移动开发 小程序 JavaScript
微信小程序学习实录6(百度经纬度采集、手动调整精度、H5嵌入小程序、百度地图jsAPI、实时定位、H5更新自动刷新)
微信小程序学习实录6(百度经纬度采集、手动调整精度、H5嵌入小程序、百度地图jsAPI、实时定位、H5更新自动刷新)
212 1
|
存储 人工智能 前端开发
前端合成海报并保存到本地
前端合成海报并保存到本地
72 0
|
数据可视化 前端开发 搜索推荐
数据可视化大屏百度地图GPS轨迹位置感知状态开发实战案例解析(百度地图jsAPI,包含缩放控件、点线覆盖物、弹窗、标注图标分类功能)
数据可视化大屏百度地图GPS轨迹位置感知状态开发实战案例解析(百度地图jsAPI,包含缩放控件、点线覆盖物、弹窗、标注图标分类功能)
261 0
|
11月前
[笔记]黑科技:文件隐藏在图片里
[笔记]黑科技:文件隐藏在图片里
|
JSON JavaScript 定位技术
python实现直接读取excle数据实现的百度地图标注
python实现直接读取excle数据实现的百度地图标注
106 0
|
定位技术
百度标注地图markers图片icon不正常显示的样式冲突解决方案
百度标注地图markers图片icon不正常显示的样式冲突解决方案
153 0
|
前端开发
如何实现一个图片的伪预览效果
如何实现一个图片的伪预览效果
67 0