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

问题描述 投票:0回答:1

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

提前致谢。

ios swift apple-watch health-kit hksamplequery
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.