iOS Swift, как различить шаги с помощью Apple Watch и iPhone? - PullRequest
0 голосов
/ 17 сентября 2018

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

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

1 Ответ

0 голосов
/ 18 сентября 2018

Используйте .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)
...