ios 步数和卡路里燃烧与HealthKit,Apple Watch不匹配

eyh26e7m  于 2023-05-02  发布在  iOS
关注(0)|答案(1)|浏览(230)

我们正在尝试使用**HealthKit从Health中检索每日步数和卡路里燃烧数据。**使用的方法为:

stepCount = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)

caloriesBurn = HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)

和,使用执行的查询,

HKSampleQuery(sampleType: <stepCount or caloriesBurn>, predicate: last24hPredicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil)

但是,根据不同的情况,我们在使用HealthKit进行检索时会面临数据不一致的问题。

**快乐的情况:**如果我们在进行日常活动和数据时仅依赖于iPhone,然后使用HealthKit检索到我们的应用程序中,则步数和卡路里燃烧数据与健康应用程序中显示的相同。
**可悲的情况:**如果我们在进行日常活动时使用Apple Watch,步数和卡路里燃烧都不会正确进入我们的应用程序,总是数据不匹配。

由于大多数客户都在使用小工具来跟踪他们的活动,健康应用程序和我们的应用程序之间的数据不匹配是不可接受的。
请提出更好的解决方案来克服这些问题。
附上一些健康,健身和我们的应用程序的截图,以显示数据不匹配的情况。
1.健康应用程序中的步数

1.健身app中的步数和卡路里消耗

1.步数和卡路里燃烧在我们的应用程序

我们需要显示完全相同的健康应用程序的所有参数的值到我们自己的应用程序,特别是专注于步数,卡路里燃烧。

nzk0hqpo

nzk0hqpo1#

我使用了HKStatisticsQuery来解决这个问题。

let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)
            
let query = HKStatisticsQuery(quantityType: energyType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, statisticsOrNil, _) in
                
  guard let statistics = statisticsOrNil else {
     return
  }

  // perform further operation
}

相关问题