HealthKit Swift getting today's steps

后端 未结 6 1494
灰色年华
灰色年华 2020-12-23 11:50

I am making a swift iOS app that integrates with a user\'s step count as reported by the Health app. I can easily find the user\'s step count in the last hour, using this as

6条回答
  •  情话喂你
    2020-12-23 12:29

    Here is the right way using HKStatisticsCollectionQuery courtesy of the direction from the code above.

    This is written in Swift 3 so you may have to convert some of the code back to 2.3 or 2 if not on 3 yet.

    Swift 3

     func retrieveStepCount(completion: (stepRetrieved: Double) -> Void) {
    
            //   Define the Step Quantity Type
            let stepsCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
    
            //   Get the start of the day         
            let date = Date()
            let cal = Calendar(identifier: Calendar.Identifier.gregorian)
            let newDate = cal.startOfDay(for: date)
    
            //  Set the Predicates & Interval
            let predicate = HKQuery.predicateForSamples(withStart: newDate, end: Date(), options: .strictStartDate)
            var interval = DateComponents()
            interval.day = 1
    
            //  Perform the Query
            let query = HKStatisticsCollectionQuery(quantityType: stepsCount!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: newDate as Date, intervalComponents:interval)
    
            query.initialResultsHandler = { query, results, error in
    
                if error != nil {
    
                    //  Something went Wrong
                    return
                }
    
                if let myResults = results{
                    myResults.enumerateStatistics(from: self.yesterday, to: self.today) {
                        statistics, stop in
    
                        if let quantity = statistics.sumQuantity() {
    
                            let steps = quantity.doubleValue(for: HKUnit.count())
    
                            print("Steps = \(steps)")
                            completion(stepRetrieved: steps)
    
                        }
                    }
                }
    
    
            }
    
            storage.execute(query)
        }
    

    Objective-C

    HKQuantityType *type = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    
    NSDate *today = [NSDate date];
    NSDate *startOfDay = [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian] startOfDayForDate:today];
    
    NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startOfDay endDate:today options:HKQueryOptionStrictStartDate];
    NSDateComponents *interval = [[NSDateComponents alloc] init];
    interval.day = 1;
    
    HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:type quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum anchorDate:startOfDay intervalComponents:interval];
    
    query.initialResultsHandler = ^(HKStatisticsCollectionQuery * _Nonnull query, HKStatisticsCollection * _Nullable result, NSError * _Nullable error) {
      if (error != nil) {
        // TODO
      } else {
        [result enumerateStatisticsFromDate:startOfDay toDate:today withBlock:^(HKStatistics * _Nonnull result, BOOL * _Nonnull stop) {
          HKQuantity *quantity = [result sumQuantity];
          double steps = [quantity doubleValueForUnit:[HKUnit countUnit]];
          NSLog(@"Steps : %f", steps);
        }];
      }
    };
    
    [self.storage executeQuery:query];
    

提交回复
热议问题