简体   繁体   中英

How can i get daily average steps in minute form healthkit

Here is my code:

HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

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

How can i get daily average steps in minute form health kit?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM