What's new in HealthKit
Description: Bring the latest HealthKit features to your health & fitness app. We'll show you how to capture more detailed sleep data through sleep stages, track swim-bike-run and interval workouts with the enhanced Workout API, and save vision prescriptions — including an image of the physical prescription — directly to HealthKit while preserving privacy.
Sleep analysis
- Introducing sleep stages
- Apple Watch will automatically track all the different sleep stages you go through when you're asleep
- this data will be accessible from the Health app and saved in HealthKit
- apps can read/write sleep analysis data via
HKCategoryTypeIdentifier.sleepAnalysis
- three sleep stages: REM, core, deep
public enum HKCategoryValueSleepAnalysis: Int {
case inBed
case asleep // ❌ removed
case asleepUnspecified // 👈🏻 Replaces asleep, used when user is asleep but no sleep stage is specified
case awake
case asleepCore // 👈🏻 New
case asleepDeep // 👈🏻 New
case asleepREM // 👈🏻 New
}
- new predicate
HKCategoryValuePredicateProviding
to access HealthKit samples, for example:
let stagePredicate = HKCategoryValueSleepAnalysis.predicateForSamples(.equalTo, value: .asleepREM)
let queryPredicate = HKSamplePredicate.sample(type: HKCategoryType(.sleepAnalysis), predicate: stagePredicate)
let sleepQuery = HKSampleQueryDescriptor(predicates: [queryPredicate], sortDescriptors: [])
// Run the query
let sleepSamples = try async sleepQuery.result(for: healthStore) // 👈🏻 available since iOS 15.4
Swift async
- available since iOS 15.4
- see types that conform to the new
HKAsyncQuery
to see which queries support swift concurrency
Workouts
- new workouts API in iOS 16 and watchOS 9 to make it possible to capture mixed activities workouts like triathlon
- each activity is represented via one
HKWorkoutActivity
- each
HKWorkoutActivity
comes with- its own
HKWorkoutConfiguration
, which includes its activity type - an array of
HKWorkoutEvent
- its own
- you can read statistics for each
HKWorkoutActivity
separately - workout activities are not required to be contiguous
- between activities you can create a
HKWorkoutActivity
with typeHKWorkoutActivityType.transition
- all activities will be put together under the same
HKWorkout
object - new predicates for querying only a specific type of activity within workouts, e.g.,
predicateForWorkoutActivities(operatorType:quantityType:averageQuantity:)
Heart rate recovery
- new Cardio Recovery data type
HKQuantityTypeIdentifier.heartRateRecoveryOneMinute
- additional context can be added as metadata
Vision prescriptions
- from iOS 16, we can save glasses and contacts prescriptions in HealthKit
HKObjectType.visionPrescriptionType()
- optionally we can store digital copy of the physical prescription
- new
HKVisionPrescription
class, along withHKGlassesLensSpecification
andHKGlassesPrescription
specification objects