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.sleepAnalysisthree 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
HKCategoryValuePredicateProvidingto 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.4Swift async
available since iOS 15.4
see types that conform to the new
HKAsyncQueryto 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
HKWorkoutActivityeach
HKWorkoutActivitycomes withits own
HKWorkoutConfiguration, which includes its activity typean array of
HKWorkoutEvent
you can read statistics for each
HKWorkoutActivityseparatelyworkout activities are not required to be contiguous
between activities you can create a
HKWorkoutActivitywith typeHKWorkoutActivityType.transitionall activities will be put together under the same
HKWorkoutobjectnew 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.heartRateRecoveryOneMinuteadditional 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
HKVisionPrescriptionclass, along withHKGlassesLensSpecificationandHKGlassesPrescriptionspecification objects
