Apple is introducing a category of metrics to HealthKit that captures the complex and important elements of human movement, all via iPhone (8 and later) and Apple Watch (4 and later).
New Mobility metrics on iPhone
Walking speed (
.walkingspeed): Rate of travel over flat groundStep length (
.walkingStepLength): Distance between feet when walkingDouble support time (
.walkingDoubleSupportPercentage): Percentage of time with two feet on the groundWalking asymmetry (
.walkingAsymmetryPercentage): Percentage of walking when left and tight step time are different
New Mobility metrics on Apple Watch
Stair speed up (
.stairAscentSpeed): Rate of vertical ascent on stairsStair speed down (
.stairDescentSpeed): Rate of vertical descent on stairsSix-minute walk (
.sixMinuteWalkTestDistance): Weekly estimate of distance that could be walked in six minutes on flat ground
How to read HealthKit data
Request authorization:
/// Request authorization from the user
self.healthStore = HKHealthStore()
let typesToRead: Set = [
HKObjectType.quantityType(forIdentifier:-walkingSpeed)!
]
self.healthStore.requestAuthorization(toShare: nil, read: typesToRead) { success, error in
if !success, let error = error {
print("Authorization failed: \(error.localizedDescription)")
}
}Fetch the data of interest in the relevant time range:
// Select a time range
let start = Calendar. current.date(byAdding: .day, value: -30, to: dateOfInjury)
let end = Calendar.current.date(byAdding: .day, value: 60, to: dateOfInjury)
let datePredicate = HKQuery.predicateForSamples(withStart: start, end: end, options: [])
// Query walking speeds
let walkSpeedType = HKSampleType.quantityType(forIdentifier: .walkingSpeed)!
let sortByStartDate = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)
let query = HKSampleQuery(
sampleType: walkSpeedType,
predicate: datePredicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: [sortByStartDate]) { _, samples, _ in
visualizeWalkSpeeds(samples)
}
self.healthStore.execute(query) 