如何以分钟形式healthkit获得每日平均步数

时间:2015-11-20 11:21:13

标签: ios

这是我的代码:

HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:[[NSDate date] dateByAddingTimeInterval:60*1] endDate:[NSDate date] options:HKQueryOptionStrictStartDate];

如何获得分钟保健套件中的每日平均步数?

1 个答案:

答案 0 :(得分:0)

 
- (void)getAvgStepsCount{
   HKHealthStore *healthStore = [[HKHealthStore alloc] init];

   if ([HKHealthStore isHealthDataAvailable]) {
      HKQuantityType *stCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
      NSSet *readDataTypes = [NSSet setWithObjects:stCountType, nil];

     [healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
        if (!success) {
            return;
        }
    }];
  }

  HKQuantityType *qType = [HKQuantityType
                        quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount
                        ];

  NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:yesterdayDate endDate:todayDate options:HKQueryOptionStrictStartDate];

  HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:qType
                                                       predicate:predicate
                                                           limit:1
                                                 sortDescriptors:nil
                                                  resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
    if (!results)
        return;

    HKQuantitySample *quantitySample = results.firstObject;
    HKQuantity *quantity = quantitySample.quantity;

    HKUnit *stepsUnit = [HKUnit countUnit];
    double avgStepsInMinute = [quantity doubleValueForUnit:stepsUnit]/24/60;

}];

[healthStore executeQuery:query];}