Search code examples
iosiphonewatchkitapple-watchcore-motion

Aggregated CMPedometerData (iPhone + Watch total count)


My app collects CMPedometerData both on the iPhone and the Watch.

iPhone's steps number (both in real time and historical data) are significantly lower than those recorded by the Watch (witch makes sense, since "on device" steps are actually lower when you always wear the Watch while sometimes leaving the phone on the desk).

The fact is, the Watch seems to have the aggregated data, or at least since it's the higher step count the most significant data, so I'd like to have those aggregated / higher data on the iPhone app too.

Do I need to collect them on both devices and merge them manually? Is there a way to get the aggregated data from CMPedometer, since Apple is already doing the merge behind the scenes?

Thanks in advance!


Solution

  • HealthKit uses HKStatisticsQuery and HKStatisticsCollectionQuery to compute the aggregated step count from several devices. These methods automatically detect overlaps in the measurements and get rid of the duplicate entries.

    You can find some sample in in the official documentation of HKStatisticsQuery.