Get Started
Requirements
Minimum requirements on Asleep SDK for iOS
- Xcode 14.1 or later
- iOS 14.0 or later
- Swift 5.0 or later
API Key
An API key is required to use AsleepTrack SDK.
Regarding how to issue an API key, please refer to Generate API key.
Getting Ready
AsleepTrack SDK for iOS is distributed via the native Swift Package Manager built into Xcode.
Install AsleepTrack 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.
Import the Asleep SDK
import AsleepSDK
Sleep Tracking with AsleepTrack SDK
Initialize the AsleepTrack 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
Create SleepTrackingManager
var config: Asleep.Config?
let delegate: AsleepSleepTrackingManagerDelegate = self
var manager: Asleep.SleepTrackingManager?
if let config {
manager = Asleep.createSleepTrackingManager(config: config,
delegate: self)
}
You must use the Asleep.Config issued in userDidJoin.
Start Tracking
var manager: Asleep.SleepTrackingManager?
manager?.startTracking()
SDK needs microphone permission for startTracking()
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 } }
Create Reports
var config: Asleep.Config?
var reports: Asleep.Reports?
if let config {
reports = Asleep.createReports(config: config)
}
You must use the Asleep.Config issued in userDidJoin.
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)
}
}
}
Run the Sample App to quickly familiarize yourself with the SDK and apply it to your actual project.
https://github.com/asleep-ai/asleep-sdk-io~~s-sampleapp-public~~
Updated 11 days ago