按天统计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 {
                        //授权失败
                    }
                }];
            }


你可能感兴趣的:(HealthKit)