Sleep Cycle SDK - iOS Documentation
Overview
The Sleep Cycle SDK for iOS enables developers to integrate advanced sleep analysis capabilities into their applications. The SDK provides real-time sleep tracking using audio and motion sensors, delivering detailed sleep insights, stage transitions, and detected events throughout the night.System Requirements
Minimum iOS Version:- iOS: 16.0+
- macOS: 13.0+
- Swift Version: 5.9+
- The SDK is written in Swift and provides a Swift-native API with async/await support
Installation
Swift Package Manager
Add the Sleep Cycle SDK to your project using Swift Package Manager:- In Xcode, select File → Add Package Dependencies
- Enter the package URL:
https://github.com/MDLabs/sleepcycle-sdk-swift - Select the version you want to use (Semantic Versioning)
- Add the package to your target
Package.swift:
The SDK requires an API key for authorization. Contact Sleep Cycle to obtain credentials.
Prerequisites
Permissions
The SDK requires microphone access for audio-based sleep analysis. Add the following to yourInfo.plist:
Background modes
To ensure continuous sleep analysis throughout the night, enable the appropriate background modes in your app’s capabilities:- Open your project in Xcode
- Select your app target
- Go to “Signing & Capabilities”
- Add “Background Modes” capability
- Enable “Audio”
Info.plist:
General
It’s typical to callinitialize() during application startup, often in your app’s initialization phase or in a SwiftUI App struct. This ensures the SDK is ready when your views need it. The SDK maintains its state across the app lifecycle.
The SDK is thread-safe and uses Swift concurrency (async/await) for all asynchronous operations.
Initialize the SDK
The SDK requires authentication before use. The initialization process validates your credentials and determines available features.logLevel: Controls SDK logging verbosity (.error,.info,.debug)logger: Optional custom loggerapiKey: API key used for authorization
- Returns
SleepAnalysisFeatureson success and transitions toSdkState.initialized. - Throws on authorization failure.
Start a sleep analysis session
Once initialized, you can start a sleep analysis session:config: Configuration object specifying which sensors to useat: The start time for the analysis (defaults to current time)using: OptionalDataSource(e.g., live or file replay)
Stop a session
To stop an active analysis session and retrieve the results:Real-time events
The SDK provides real-time updates viaAsyncStream publishers:
Audio clips
The SDK can capture short audio recordings when specific sleep events are detected, such as snoring, sleep talking, or coughing. Create anAudioEventListener using AudioClipsConfig and AudioClipsReceiver:
AudioClip contains audio samples ([Float]), metadata, and event context. Use custom listeners (conforming to AudioEventListener) for advanced audio workflows.
Monitor SDK state
Monitor SDK state changes using the AsyncStream:Public data types
-
SleepAnalysisConfig: Configures sensors used during analysis.
useAudio: BooluseAccelerometer: Bool
-
SleepAnalysisFeatures: Feature flags returned on successful initialization.
sleepStaging: Bool,smartAlarm: Bool,audioEvents: Bool,snoringDetection: Bool
-
AnalysisResult: Returned by
stopAnalysis(at:).startDate: Date,endDate: Date,statistics: SleepStatistics,events: [EventType: [Event]]?
-
SleepStatistics: Aggregated KPIs computed for the session.
sessionInterval: DateIntervaltotalSleepDuration: TimeInterval?,sleepOnsetLatency: TimeInterval?,sleepEfficiency: Double?finalWakeTime: Date?,numberOfAwakenings: Int?sleepStages: [(SleepStage, DateInterval)]?,sleepStageDurations: [SleepStage: TimeInterval]?snoreSessions: [SnoreSession]?,snoreTime: TimeInterval?
-
EventType: The kind of detected event (e.g.,
.snoring,.talking,.movement). -
Event: A detected event with timing and confidence.
type: EventType,interval: DateInterval,probability: Double,source: EventSource
-
SdkState: SDK lifecycle state.
.uninitialized,.initialized,.running
- SleepCycleSdkError: Error cases thrown by SDK APIs.
- DataSource: Protocol for providing custom audio/motion input to the SDK.