Get Started
Follow the guideline for testing the sleep tracking
To accurately test Asleep's sleep tracking/analysis, please follow the test environment guide. Please note that sleep analysis results obtained in environments not adhering to this guide may not accurately reflect actual sleep patterns.
- Sleep measurement begins analysis when at least 20 minutes of audio is uploaded, after which it is analyzed every 5 minutes or 20 minutes depending on the plan (contract terms).
🔗 Check Test Environment Guideline
1. Requirements
1.1 Minimum requirements on Asleep SDK for iOS
- Xcode 14.1 or later
- iOS 14.0 or later
- Swift 5.0 or later
1.2 API Key
An API key is required to use Asleep SDK.
Regarding how to issue an API key, please refer to Generate API key.
2. Getting Ready
Asleep SDK for iOS is distributed via the native Swift Package Manager built into Xcode.
2.1 Install Asleep SDK
-
In Xcode, click File > Add Packages.
-
In the dialog that appears, enter the repository URL https://github.com/asleep-ai/asleep-sdk-ios.
-
In Version, select Up to Next Major Version and the default option.
-
Click
Add Package
button.
-
Click
Add Package
button one more time.
-
In Xcode, click
Signing & Capabilities
on your application target.
-
Click
+ Capability
button and double clickBackground Modes
.
-
Select
Audio, AirPlay, and Picture in Picture
.
-
In Xcode, click
Info
on your app target.
-
Click
+
and addPrivacy - Microphone Usage Description
.
-
Write a microphone permission request message.
2.2 Import the Asleep SDK
import AsleepSDK
3. Sleep Tracking with Asleep SDK
3.1 Step 1: Initialize the Asleep SDK
-
Initialize config
- If userId is nil, a new userId will be generated.
- Enter the proxy server in baseUrl. If nil, the default base URL will be used.
- Enter the server to receive the analysis results directly in callbackUrl. If nil, there is no callback.
- If there is a nickname for the app you are developing, enter it in the service.
let apiKey: String = "YOUR_API_KEY" let userId: String? let baseUrl: URL? let callbackUrl: URL? let service: String? let delegate: AsleepConfigDelegate = self Asleep.initAsleepConfig(apiKey: apiKey, userId: userId, baseUrl: baseUrl, callbackUrl: callbackUrl, service: service, delegate: self)
-
Get config and user ID
extension YourClass: AsleepConfigDelegate { func userDidJoin(userId: String, config: Asleep.Config) { self.userId = userId self.config = config } func didFailUserJoin(error: Asleep.AsleepError) { print(error) } func userDidDelete(userId: String) { print("\(userId) is deleted") } }
Should save
userId
in your permanent storage for join
3.2 Step 2: Create SleepTrackingManager
var config: Asleep.Config?
let delegate: AsleepSleepTrackingManagerDelegate = self
var manager: Asleep.SleepTrackingManager?
if let config {
manager = Asleep.createSleepTrackingManager(config: config,
delegate: self)
}
3.3 Step 3: Start Tracking
var manager: Asleep.SleepTrackingManager?
manager?.startTracking()
SDK needs microphone permission for startTracking()
3.4 Step 4: Stop Tracking
var manager: Asleep.SleepTrackingManager?
manager?.stopTracking()
- Get session ID for the sleep report
extension YourClass: AsleepSleepTrackingManagerDelegate { func didClose(sessionId: String) { self.sessionId = sessionId } }
3.5 Step 5: Create Reports
var config: Asleep.Config?
var reports: Asleep.Reports?
if let config {
reports = Asleep.createReports(config: config)
}
3.6 Step 6: Get Report
var reports: Asleep.Reports?
var sessionId: String?
if let reports, let sessionId {
// Closure
reports.report(sessionId: sessionId) {
if let error = $1 {
print(error)
return
}
if let report = $0 {
}
}
// Async
Task {
do {
let report: Asleep.Model.Report = try await reports.report(sessionId: sessionId)
} catch {
print(error)
}
}
}
Updated over 1 year ago