I am wondering how to find a user's top speed using HealthKit.
I have come across this HKMetadataKeyMaximumSpeed but have no idea how to implement/use it. The docs are not very clear to me.
Can anyone provide any help here?
I am wondering how to find a user's top speed using HealthKit.
I have come across this HKMetadataKeyMaximumSpeed but have no idea how to implement/use it. The docs are not very clear to me.
Can anyone provide any help here?
Apple indicates thath :
HKMetadataKeyMaximumSpeed Set this key on a workout, workout segment, or a quantity sample that represents distance. Set its value to an HKQuantity object with a length/time unit (for example, m/s)
So you can use this function with this property :
func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate event: HKWorkoutEvent) {
if event.type == .segment {
let maxSpeed = event.metadata?[HKMetadataKeyMaximumSpeed]
// do it here your logic
}
Regarding to your comment: You can not genererate these events on the simulator.
There is example provided by Apple in docs.
func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate event: HKWorkoutEvent) {
// Check to see if the event is a segment.
if event.type == .segment {
// Get the segment's start and end times.
let startTime = event.dateInterval.start
let endTime = event.dateInterval.end
// Get the segment's metadata.
let averageSpeed = event.metadata?[HKMetadataKeyAverageSpeed]
let maxSpeed = event.metadata?[HKMetadataKeyMaximumSpeed]
let alpineSlope = event.metadata?[HKMetadataKeyAlpineSlopeGrade]
let elevationAscended = event.metadata?[HKMetadataKeyElevationAscended]
let elevationDescended = event.metadata?[HKMetadataKeyElevationDescended]
// Do something with the segment data here...
}
// Also handle any other event types...
}