iOS Swift, как различать шаги с помощью Apple Watch и iPhone?

Я использую структуру HealthKit для получения шагов от пользователя с помощью HKSource. В Xcode Objective C я использовал Bundle Identifier, чтобы отличать шаги от часов/iPhone. Но с помощью Swift я не смог этого сделать. Пожалуйста, предложите.

Заранее спасибо.


person Suresh Durishetti    schedule 17.09.2018    source источник


Ответы (1)


Используйте .separateBySource в параметрах вашего запроса. Например:

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)
person ielyamani    schedule 17.09.2018