IOSBackgroundDownloadWorker
Background download worker using NSURLSession's background session mechanism.
Why this exists
BGTaskScheduler tasks die with the app process (30-second iOS limit). NSURLSession background downloads are handed to an iOS OS daemon — they survive complete app termination, charge on a locked device, and hand the file back to your app when done.
Usage
val kmpWorker = IOSKmpWorker(eventStore = myStore)
// In your Swift/Compose Multiplatform UI:
kmpWorker.backgroundDownloads.download(
taskId = "user-avatar",
url = "https://cdn.example.com/avatar.jpg",
onComplete = { path -> println("Downloaded to $path") },
onError = { error -> println("Failed: $error") }
)Content copied to clipboard
Required AppDelegate wiring (Swift)
func application(
_ application: UIApplication,
handleEventsForBackgroundURLSession identifier: String,
completionHandler: @escaping () -> Void
) {
IOSBackgroundDownloadWorker.handleBackgroundSession(
identifier: identifier,
completionHandler: completionHandler
)
}Content copied to clipboard
Observation
Download states are emitted to TaskMonitor and persisted via EventStore:
TaskState.Scheduled— download enqueued with OSTaskState.Success— file downloaded, path via onComplete callbackTaskState.Failed— network error, see onError callback
Parameters
eventStore
Optional. If provided, Success/Failed states are persisted for cold-launch replay.