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
}
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

Heart rate recovery

Vision prescriptions

Missing anything? Corrections? Contributions are welcome 😃

Written by

Federico Zanetello

Federico Zanetello

Software engineer with a strong passion for well-written code, thought-out composable architectures, automation, tests, and more.