HealthKit Swift获取今天的步数

37

我正在制作一款快速的iOS应用程序,与用户在健康应用中报告的步数集成。我可以轻松地找到用户在过去一小时内的步数,使用以下内容作为我的谓词:

let anHourBeforeNow: NSDate = NSDate().dateByAddingTimeInterval(-60 * 60)
let predicate = HKQuery.predicateForSamplesWithStartDate(anHourBeforeNow, endDate: NSDate(), options: .None)

我已经把其他内容处理好了,所以我能够成功地访问用户过去一小时的步数。但是,我如何访问用户当天从开始到现在的步数数据,就像健康应用程序在步骤部分显示的那样?

我正在尝试做这样的事情:

let date = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)
let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None)

但是这段代码没有考虑时区(因此它给出的是UTC的一天开始,而不是用户所在地区的一天开始),而且我也得到了高度虚高的步数统计(原因不明)。

那么我该怎么样才能获得用户当前日期的步数,与Health报告的步数相同,就像这张图片中显示的那样:enter image description here


这可以帮助您查看 HealthKit 中每个日期的总步数。https://dev59.com/N10b5IYBdhLWcg3wIOF9 - Victor Sigler
你应该包含查询代码和如何计算步数的方法,这样我们才能帮助你解决这个问题。如果你发现自己犯了同样的错误,可以参考我的回答:https://dev59.com/RJXfa4cB1Zd3GeqPmvjG。 - Allan
在你的应用程序中,NSTimeZone.localTimeZone() 返回什么? - Allan
答案可以在这里找到:https://dev59.com/RJXfa4cB1Zd3GeqPmvjG - owlswipe
6个回答

56

当您想要检索一段时间内的数据时,最好使用HKStatisticsCollectionQuery。 如果只想获取特定日期的步数,请使用HKStatisticsQuery。

Swift 5:

let healthStore = HKHealthStore()

func getTodaysSteps(completion: @escaping (Double) -> Void) {
    let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
    
    let now = Date()
    let startOfDay = Calendar.current.startOfDay(for: now)
    let predicate = HKQuery.predicateForSamples(
        withStart: startOfDay,
        end: now,
        options: .strictStartDate
    )
    
    let query = HKStatisticsQuery(
        quantityType: stepsQuantityType,
        quantitySamplePredicate: predicate, 
        options: .cumulativeSum
    ) { _, result, _ in
        guard let result = result, let sum = result.sumQuantity() else {
            completion(0.0)
            return
        }
        completion(sum.doubleValue(for: HKUnit.count()))
    }
    
    healthStore.execute(query)
}

4
这个解决方案比被接受的方案更简单、更高效。 - chengsam
@Jose,你能否回答一下这个问题:https://dev59.com/alUL5IYBdhLWcg3wQGHd - Jack
1
当然,我已经发布了一个例子。 - José
我们能否分别获取跑步和步行步数的计数? - Prashant Tukadiya
@PrashantTukadiya 是的,你只需要使用不同的quantityType。 - José

32

这是正确的方法,使用HKStatisticsCollectionQuery从上面的代码中获得指示。

此代码是用Swift 3编写的,如果您还未升级到3,则可能需要将一些代码转换回2.3或2。

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

不,我想获取iPhone检测到的所有步数,并自动添加到其他跟踪器中。但我不想获取用户手动添加到健康应用程序中的那些步数。 - Byte
1
我其实还不知道怎么做!我会进行一些研究,并尽快回复您。 - Gugulethu
@Gugulethu,你能否将你的答案转换为Swift 2并发布吗? - Byte
使用HKStatisticsCollectionQuery仅仅是为了获取今天的步数有点过头了。可以使用HKStatisticsQuery来代替。 - José
2
self.yesterday和self.today是从哪里来的? - Capotasto
显示剩余5条评论

6
如果您想在一段时间内获取每天的步数总数,则可以使用Swift 4.2中的HKStatisticsCollectionQuery
let startDate = Date().addingTimeInterval(-3600 * 24 * 7)
let endDate = Date()

let predicate = HKQuery.predicateForSamples(
  withStart: startDate,
  end: endDate,
  options: [.strictStartDate, .strictEndDate]
)

// interval is 1 day
var interval = DateComponents()
interval.day = 1

// start from midnight
let calendar = Calendar.current
let anchorDate = calendar.date(bySettingHour: 12, minute: 0, second: 0, of: Date())

let query = HKStatisticsCollectionQuery(
  quantityType: HKSampleType.quantityType(forIdentifier: .stepCount)!,
  quantitySamplePredicate: predicate,
  options: .cumulativeSum,
  anchorDate: anchorDate!,
  intervalComponents: interval
)

query.initialResultsHandler = { query, results, error in
  guard let results = results else {
    return
  }

  results.enumerateStatistics(
    from: startDate,
    to: endDate,
    with: { (result, stop) in
      let totalStepForADay = result.sumQuantity()?.doubleValue(for: HKUnit.count()) ?? 0
    }
  )
}

store.execute(query)

5

您正在使用的查询获取了用户在Healthkit中记录的所有步数,而没有像Health应用程序那样智能地过滤重复的步数。相反,您希望获取Health应用程序在组合来自不同来源的步数后生成的聚合步数数据,以获得准确的计数。

为此,您可以使用以下代码:

func recentSteps2(completion: (Double, NSError?) -> () )
{ // this function gives you all of the steps the user has taken since the beginning of the current day.

    checkAuthorization() // checkAuthorization just makes sure user is allowing us to access their health data.
    let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) // The type of data we are requesting


    let date = NSDate()
    let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let newDate = cal.startOfDayForDate(date)
    let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None) // Our search predicate which will fetch all steps taken today

    // The actual HealthKit Query which will fetch all of the steps and add them up for us.
    let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
        var steps: Double = 0

        if results?.count > 0
        {
            for result in results as! [HKQuantitySample]
            {
                steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
            }
        }

        completion(steps, error)
    }

    storage.executeQuery(query)
}

4
抱歉,但这是不正确的。HKSampleQuery会返回每个步骤记录(它不会删除重复项),并且计算匹配谓词的样本总数的效率较低。您应该使用HKStatisticsQuery。 - Allan

5

针对 Swift 4.2

1) 获取 HealthKit 权限

import HealthKit

func getHealthKitPermission() {

    delay(0.1) {
        guard HKHealthStore.isHealthDataAvailable() else {
            return
        }

        let stepsCount = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!

        self.healthkitStore.requestAuthorization(toShare: [], read: [stepsCount]) { (success, error) in
            if success {
                print("Permission accept.")
            }
            else {
                if error != nil {
                    print(error ?? "")
                }
                print("Permission denied.")
            }
        }
    }
}

2) 获取特定日期的步数

func getStepsCount(forSpecificDate:Date, completion: @escaping (Double) -> Void) {
        let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
        let (start, end) = self.getWholeDate(date: forSpecificDate)

        let predicate = HKQuery.predicateForSamples(withStart: start, end: end, options: .strictStartDate)

        let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
            guard let result = result, let sum = result.sumQuantity() else {
                completion(0.0)
                return
            }
            completion(sum.doubleValue(for: HKUnit.count()))
        }

        self.healthKitStore.execute(query)
    }

    func getWholeDate(date : Date) -> (startDate:Date, endDate: Date) {
        var startDate = date
        var length = TimeInterval()
        _ = Calendar.current.dateInterval(of: .day, start: &startDate, interval: &length, for: startDate)
        let endDate:Date = startDate.addingTimeInterval(length)
        return (startDate,endDate)
    }

使用方法

self.getStepsCount(forSpecificDate: Date()) { (steps) in
                if steps == 0.0 {
                    print("steps :: \(steps)")
                }
                else {
                    DispatchQueue.main.async {
                        print("steps :: \(steps)")
                    }
                }
            }

1

使用上述答案和苹果官方文档:https://developer.apple.com/reference/healthkit/hkstatisticsquery 我在Xcode 8.0上的Swift 2.3中实现了以下内容。

class func getStepCountHealth(startDate: NSDate, endDate: NSDate, completion:(Double?, NSError?)->()) {

            let healthKitStore:HKHealthStore = HKHealthStore()

            //   Define the sample type
            let sampleType = HKQuantityType.quantityTypeForIdentifier(
            HKQuantityTypeIdentifierStepCount)

            //  Set the predicate
            let predicate = HKQuery.predicateForSamplesWithStartDate(startDate,
                                                                 endDate: endDate, options: .None)
            // build the query
            let sampleQuery = HKStatisticsQuery(quantityType: sampleType!,
                                      quantitySamplePredicate: predicate,
                                      options: .CumulativeSum) { query, results, error in

                                        if results != nil {
                                            let quantity = results?.sumQuantity()
                                            let unit = HKUnit.countUnit()
                                            let totalSteps = quantity?.doubleValueForUnit(unit)
                                            completion(totalSteps, error)
//                                            print("totalSteps for \(endDate) are \(totalSteps!)")
                                        } else {
                                            completion(nil, error)
//                                            print("results are nil")
                                            return
                                        } 
                                    }
            // execute the Query
            healthKitStore.executeQuery(sampleQuery)
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接