Ada*_*erg 4 heartbeat swift healthkit watchos
我正在向 HKStore 分享一项锻炼,并要求阅读以下类型:
let typesToRead: Set = [
HKQuantityType.quantityType(forIdentifier: .heartRate)!,
HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)!
]
Run Code Online (Sandbox Code Playgroud)
在我的锻炼课程(跟踪和记录活动锻炼)中,我能够记录实时数据并将其保存到 HealthStore,但我不知道如何读取该数据并将其显示在屏幕上以供用户在其中查看实时数据锻炼。(或者至少在控制台中打印心率、activeEnergyBurned 等数据)。
这是似乎正在共享相关锻炼数据的 HKLiveWorkoutBuilderDelegate
// MARK: HKLiveWorkoutBuilderDelegate
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
for type in collectedTypes {
guard let quantityType = type as? HKQuantityType else {
return // Nothing to do
}
let statistics = workoutBuilder.statistics(for: quantityType)
let typeDescription = type.description
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的锻炼的激活方式:
override func awake(withContext context: Any?) {
super.awake(withContext: context)
configuration.activityType = .running
configuration.locationType = .indoor
do {
session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
builder = session.associatedWorkoutBuilder()
} catch {
dismiss()
return
}
// Setup session and builder
session.delegate = self
builder.delegate = self
builder.dataSource = HKLiveWorkoutDataSource(healthStore: healthStore, workoutConfiguration: configuration)
// Start session and builder
session.startActivity(with: Date())
builder.beginCollection(withStart: Date()) { (success, error) in
self.setDurationTimerDate()
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我使用的是苹果新的 Beta WatchOS 5.0
您正在做大部分事情,但您还需要启用将数据收集到dataSource. 在我的应用程序中,我收集距离和心率数据,我这样做:
let dataSource = HKLiveWorkoutDataSource(healthStore: healthStore, workoutConfiguration: configuration)
if let hr = HKQuantityType.quantityType(forIdentifier: .heartRate) {
dataSource.enableCollection(for: hr, predicate: nil)
}
if let distance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning) {
dataSource.enableCollection(for: distance, predicate: nil)
}
workoutBuilder?.dataSource = dataSource
Run Code Online (Sandbox Code Playgroud)
然后我可以在HKLiveWorkoutBuilderDelegate方法中打印该信息,如下所示:
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
guard let hrType = HKQuantityType.quantityType(forIdentifier: .heartRate),
let distanceType = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning) else {
return
}
if collectedTypes.contains(hrType) {
if let hrQuantity = workoutBuilder.statistics(for: hrType)?.mostRecentQuantity() {
// We want to have BPM
let hrUnit = HKUnit(from: "count/min")
let hr = Int(hrQuantity.doubleValue(for: hrUnit))
debugPrint("HR: \(hr)")
}
}
if collectedTypes.contains(distanceType) {
if let distQuantity = workoutBuilder.statistics(for: distanceType)?.sumQuantity() {
// We want to have total distance in meters
let distance = distQuantity.doubleValue(for: HKUnit.meter())
debugPrint("Distance: \(distance) m")
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我进入控制台:
"Distance: 6.5 m"
"Distance: 10.4 m"
"HR: 112"
"Distance: 14.3 m"
"HR: 117"
"Distance: 20.8 m"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1074 次 |
| 最近记录: |