iOS Swift, How to differentiate the steps using Apple Watch and iPhone?

476 views Asked by At

I am using the HealthKit framework to retrieve the steps from the user using HKSource. In Xcode Objective C, I used Bundle Identifier to differentiate between steps from watch/iPhone. But using Swift I was not able to do so. Please suggest.

Thanks in Advance.

1

There are 1 answers

0
ielyamani On

Use .separateBySource in the options of your query. For example:

guard let sampleType = HKObjectType.quantityType(forIdentifier: .stepCount)
    else {
        fatalError("Couldn't create the quantityType for .stepCount")
}
let calendar = Calendar.current
var dateComponents = DateComponents()
dateComponents.day = 1

var anchorComponents = calendar.dateComponents([.day, .month, .year], from: Date())
anchorComponents.hour = 0
guard let anchorDate = calendar.date(from: anchorComponents) else {
    fatalError("Couldn't get the anchor date")
}

let stepsCumulativeQuery =
    HKStatisticsCollectionQuery(quantityType: sampleType,
                                quantitySamplePredicate: nil,
                                options: [.cumulativeSum , .separateBySource],
                                anchorDate: anchorDate,
                                intervalComponents: dateComponents)