KMP Live Activity provides a Live Activity API for Kotlin Multiplatform.
Add the KMP library to the app KMP module:
[versions]
# ...
kmpLiveActivity = "0.1.0"[libraries]
# ...
swmansion-kmpLiveActivity = { module = "com.swmansion.kmpliveactivity:kmp-live-activity", version.ref = "kmpLiveActivity" }commonMain.dependencies {
// ...
api(libs.swmansion.kmpLiveActivity)
}Export it to the KMP framework. In a baseline project, this change looks as follows:
listOf(iosArm64(), iosSimulatorArm64()).forEach { iosTarget ->
iosTarget.binaries.framework {
baseName = "ComposeApp"
isStatic = true
+ export(libs.swmansion.kmpLiveActivity)
}
}Open the iOS project in Xcode. Click the project entry in the navigator, select the app target
(usually iosApp), and open the Signing & Capabilities tab. If the Push Notifications capability is
not present, click the plus button, and double-click Push Notifications.
Edit the app target’s Info.plist file. Hover the cursor over Information Property List, click the
plus button, select Supports Live Activities, and set the value to YES.
Select File → New → Target. Select Widget Extension, and click Next. For Product Name, enter
LiveActivity. Ensure that Include Live Activity is checked and that the app target is selected for
Embed in Application. Click Finish.
Select File → Add Package Dependencies. In the search bar, enter
https://github.com/software-mansion-labs/kmp-live-activity. Set Dependency Rule to Exact Version,
and enter 0.1.0. Ensure that the main project (usually iosApp) is selected for Add to Project,
and click Add Package. Under Add to Target, select the app target. Click Add Package. Add the
package to the LiveActivity target analogously.
In the LiveActivity target, delete the LiveActivity.swift and LiveActivityLiveActivity.swift
files. Replace the content of the remaining file, LiveActivityBundle.swift, with the following:
import KMPLiveActivity
import SwiftUI
import WidgetKit
@main
struct LiveActivityBundle: WidgetBundle {
var body: some Widget {
LiveActivity()
}
}In the app target, open the file with the App implementation (usually iOSApp.swift). Import the
KMP framework (usually ComposeApp) and KMPLiveActivity. Add the following extension:
extension NativeLiveActivityManagerImpl: @retroactive NativeLiveActivityManager {}If the App implementation lacks an initializer, add it. Place the following line inside:
LiveActivityManager.shared.bind(nativeManager: NativeLiveActivityManagerImpl.shared)In a baseline project, these changes look as follows:
+ import ComposeApp
+ import KMPLiveActivity
import SwiftUI
+
+ extension NativeLiveActivityManagerImpl: @retroactive NativeLiveActivityManager {}
@main
struct iOSApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
+
+ init() {
+ LiveActivityManager.shared.bind(nativeManager: NativeLiveActivityManagerImpl.shared)
+ }
}When updating KMP Live Activity, keep the versions of the KMP library and the Swift package in sync.
Start a Live Activity:
val activityID =
LiveActivityManager.start(
LiveActivityAttributes.ContentState(/* ... */ ),
LiveActivityAttributes(/* ... */ ),
)Update a Live Activity:
LiveActivityManager.update(activityID, LiveActivityAttributes.ContentState(/* ... */))End a Live Activity:
LiveActivityManager.end(activityID, LiveActivityAttributes.ContentState(/* ... */))See the API reference and the sample app for more.
Founded in 2012, Software Mansion is a software agency with experience in building web and mobile apps. We are core React Native contributors and experts in dealing with all kinds of React Native issues. We can help you build your next dream product—hire us.
