Sleep Analysis
sleepAnalysis in Apple HealthKit. Check what the API exposes and what to look for when this data is missing or differs between apps.
API contract
- Identifier
sleepAnalysis- Type family
- category
- Identifier introduced
- iOS 8.0
- Documented category enum
HKCategoryValueSleepAnalysis
Look up the type in Swift
Use the identifier with an SDK and OS supporting this type. Check that HealthKit is available and configure authorization in your app before requesting access.
import HealthKit
let type = HKObjectType.categoryType(forIdentifier: .sleepAnalysis)What to check when the value is missing
Decode the category value using the identifier’s documented enum when available. Preserve the start and end dates independently of the category value.
An empty query can mean your read access is limited, the writer used another type or the selected time window contains no matching data. Start with an entry of a known date and check the corresponding type in the Health app.
Use supported devices and authorized data for verification. The API contract alone does not establish which third-party apps write this type.
Compare with Health Connect
Sleep session SleepSessionRecord
Health Connect groups sleep stages under a session. HealthKit sleep categories are separate intervals and in-bed intervals can overlap asleep intervals. Preserve the staging and calculate durations from the stages the display actually uses.
More in Sleep and mindfulness
API documentation checked . Examples are developer starting points based on the published API.