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

}


上一篇:TensorFlow在iOS和Mac上的使用


下一篇:CoreMotion运动与健身之监听用户运动状态