Can be installed on any iOS project, ActivityKit requires iOS 16.1 (or above) to work.
You can integrate manually by putting ActivityKitManager.swift
in your Xcode project. Make sure to enable Copy items if needed
and Create groups
.
- Add the
Push Notifications
capability to your iOS target.
- Add a
Widget Extension
target to your project and name it. For this quick start, we will name itExampleLA
- Delete the
ExampleLA.swift
andExampleLABundle.swift
files. - Replace the content of
ExampleLALiveActivity.swift
with:
import ActivityKit
import WidgetKit
import SwiftUI
struct ExampleAttributes: ActivityAttributes {
public typealias LiveWidgetStatus = ContentState
var var1: String
// here are static variable, defined *once* when initializing the Live Activity
public struct ContentState: Codable, Hashable {
var var2: String
// here are variable dependend on a state, i.e. will be modified
// modification can happen locally or through APNs (thus the Codable/Hashable)
}
}
@main
struct Widgets: WidgetBundle {
var body: some Widget {
if #available(iOSApplicationExtension 16.1, *) {
ActivityKitWidget()
}
}
}
struct ActivityKitView : View {
var var1: String
var var2: String
var body: some View {
VStack{
Text("Hello World!")
Text("Variable 1: \(var1)")
Text("Variable 2: \(var2)")
}
}
}
@available(iOSApplicationExtension 16.1, *)
struct ActivityKitWidget: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: ExampleAttributes.self) { context in
// SwiftUI Live Activity content
ActivityKitView(var1: context.attributes.var1,
var2: context.state.var2)
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
}
DynamicIslandExpandedRegion(.trailing) {
}
DynamicIslandExpandedRegion(.center) {
Text("Hello World!")
}
DynamicIslandExpandedRegion(.bottom) {
}
} compactLeading: {
Text("๐ถ")
} compactTrailing: {
Text("๐ป")
} minimal: {
Text("๐ธ")
}
.keylineTint(.white)
}
}
}
struct DemoWidget: View {
var body: some View {
ActivityKitView(var1: "Preview content 1", var2: "Preview content 2")
}
}
struct ActivityKit_Preview: PreviewProvider {
static var previews: some View {
DemoWidget()
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
- In some viewController do the following:
let attr = ExampleAttributes(var1: "Static text")
let state = ExampleAttributes.LiveWidgetStatus(var2: "Variable text")
ActivityKitManager.start(ExampleAttributes.self, attr, state)
print(ActivitiyKitManager.isAllowed)
// true/false
ActivityKitManager.count(ExampleAttributes.self, .active))
// 1
ActivityKitManager.count(ExampleAttributes.self, .dismissed))
// 2
ActivityKitManager.count(ExampleAttributes.self, .stopped))
// 0
let attr = ExampleAttributes(...)
let state = ExampleAttributes.LiveWidgetStatus(...)
let token = .none // .token for APN registration
ActivityKitManager.start(attr, state, token)
let state = ExampleAttributes.LiveWidgetStatus(...)
let id = "......."
ActivityKitManager.start(ExampleAttributes.self, state, id)
let state = ExampleAttributes.LiveWidgetStatus(...)
ActivityKitManager.start(ExampleAttributes.self, state)
ActivityKitManager.stop(ExampleAttributes.self, "ABC123"))
ActivityKitManager.stop(ExampleAttributes.self))
print(ActivityKitManager.getAPNtoken(ExampleAttributes.self))
// [ABC123 : ABC123, ..., DEF456 : DEF456]
print(ActivityKitManager.getAPNtoken(ExampleAttributes.self, "ABC123"))
// [ABC123 : ABC123]
ActivityKitManager.getState(ExampleAttributes.self, "ABC123"))
// .active / .dismissed / .stopped
ActivityKitManager.isActive(ExampleAttributes.self, "ABC123"))
ActivityKitManager.isDismissed(ExampleAttributes.self, "ABC123"))
// true/false