iOS Swift,如何区分使用Apple Watch和iPhone的步骤?

时间:2018-09-17 17:22:15

标签: ios swift apple-watch health-kit hksamplequery

我正在使用HealthKit框架使用HKSource从用户那里检索步骤。 在Xcode Objective C中,我使用了Bundle Identifier来区分手表和iPhone的步骤。但是,使用Swift无法做到这一点。请提出建议。

预先感谢。

1 个答案:

答案 0 :(得分:0)

在查询选项中使用.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)