按天统计HealthKit数据

if ([HKHealthStore isHealthDataAvailable]) {
                HKHealthStore *healthStore = [[HKHealthStore alloc] init];
                //可以自己添加统计的类型
                NSArray *typeArray = @[HKQuantityTypeIdentifierStepCount, HKQuantityTypeIdentifierDistanceWalkingRunning];
                NSMutableSet *readObjectTypes = [NSMutableSet set];
                for (NSString *name in typeArray) {
                    [readObjectTypes addObject:[HKObjectType quantityTypeForIdentifier:name]];
                }
    
                [healthStore requestAuthorizationToShareTypes:nil readTypes:readObjectTypes completion:^(BOOL success, NSError *error) {
                    if (success == YES)  {
                        //授权成功
                        
                        NSUInteger days = 10; //要统计的天数
                        NSDate *now = [NSDate date];
                        NSDate *beginDate = [now dateByAddingDays:-days];
                        
                        NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:beginDate endDate:now options:HKQueryOptionStrictStartDate];
                        NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
                        
                        for (NSString *name in typeArray) {
                            HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:name];
                            HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:HKObjectQueryNoLimit sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
                                if(!error && results) {
                                    
                                    NSUInteger count = 0;
                                    double meters = 0;
                                    NSUInteger sampleIndex = 0;
                                    
                                    NSUInteger type = 0;
                                    if ([query.sampleType.identifier isEqualToString:HKQuantityTypeIdentifierDistanceWalkingRunning]) {
                                        type = 1;
                                    }
                                    for (NSInteger index = 0; index < days; index++) {
                                        NSDate *curDate = [beginDate dateByAddingDays:index];
                                        for (;sampleIndex < results.count; sampleIndex++) {
                                            HKQuantitySample *samples = results[sampleIndex];
                                            if (samples.startDate.day == curDate.day) {
                                                if (type == 0) {
                                                    int v = (int)[samples.quantity doubleValueForUnit:[HKUnit countUnit]];
                                                    count += v;
                                                } else if(type == 1) {
                                                    meters += [samples.quantity doubleValueForUnit:[HKUnit meterUnit]];
                                                }
                                            }
                                            else {
                                                if (type == 0) {
                                                    NSLog(@"%@ step count %d", curDate.description, (int)count);
                                                    count = 0;
                                                } else if(type == 1) {
                                                    NSLog(@"%@ meters count %.2f", curDate.description, meters);
                                                    meters = 0;
                                                }
                                                break;
                                            }
                                        }
                                    }
                                } else {
                                    //error
                                }
                            }];
                            [healthStore executeQuery:sampleQuery];
                        }
                    } else {
                        //授权失败
                    }
                }];
            }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值