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

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

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

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)
Другие вопросы по тегам