ActivityKit is Apple's framework for creating Live Activities - those dynamic notifications that appear on the Lock Screen and in the Dynamic Island on iPhone 14 Pro and later models. Here's how to implement them:
Basic Setup
First, enable Live Activities in your app's Info.plist by adding the NSSupportsLiveActivities key with a value of YES.
Creating the Activity Attributes
Define a struct that conforms to ActivityAttributes to describe the data for your Live Activity:
import ActivityKit
struct DeliveryAttributes: ActivityAttributes {
public struct ContentState: Codable, Hashable {
var driverName: String
var deliveryTime: Date
var currentLocation: String
}
var orderNumber: String
var restaurant: String
}
The ActivityAttributes has two parts:
- Static data that doesn't change (like order number)
ContentState for dynamic data that updates (like delivery location)
Starting a Live Activity
To start a Live Activity:
func startDeliveryTracking() {
let initialContentState = DeliveryAttributes.ContentState(
driverName: "John",
deliveryTime: Date().addingTimeInterval(30 * 60),
currentLocation: "Restaurant"
)
let activityAttributes = DeliveryAttributes(
orderNumber: "12345",
restaurant: "Pizza Place"
)
do {
let deliveryActivity = try Activity.request(
attributes: activityAttributes,
contentState: initialContentState,
pushType: .token
)
print("Delivery Live Activity ID: \(deliveryActivity.id)")
} catch {
print("Error starting activity: \(error)")
}
}
Creating the UI
You'll need to create a Widget Extension that includes your Live Activity UI. In your widget file:
struct DeliveryLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: DeliveryAttributes.self) { context in
// Lock Screen UI
VStack {
HStack {
Text("Order #\(context.attributes.orderNumber)")
Spacer()
Text(context.state.deliveryTime, style: .timer)
}
Text("Driver: \(context.state.driverName)")
Text("Location: \(context.state.currentLocation)")
}
.padding()
} dynamicIsland: { context in
DynamicIsland {
// Expanded UI
DynamicIslandExpandedRegion(.leading) {
Text(context.attributes.restaurant)
}
DynamicIslandExpandedRegion(.trailing) {
Text(context.state.deliveryTime, style: .timer)
}
DynamicIslandExpandedRegion(.bottom) {
Text("Current: \(context.state.currentLocation)")
}
} compactLeading: {
Image(systemName: "bicycle")
} compactTrailing: {
Text(context.state.deliveryTime, style: .timer)
} minimal: {
Image(systemName: "bicycle")
}
}
}
}
Updating the Live Activity
To update the activity with new information:
func updateDeliveryLocation(activity: Activity<DeliveryAttributes>, newLocation: String) {
Task {
let updatedContentState = DeliveryAttributes.ContentState(
driverName: "John",
deliveryTime: Date().addingTimeInterval(15 * 60),
currentLocation: newLocation
)
await activity.update(using: updatedContentState)
}
}
Ending the Live Activity
When the delivery is complete:
func endDelivery(activity: Activity<DeliveryAttributes>) {
Task {
let finalContentState = DeliveryAttributes.ContentState(
driverName: "John",
deliveryTime: Date(),
currentLocation: "Delivered!"
)
await activity.end(using: finalContentState, dismissalPolicy: .default)
}
}
Important Considerations
- Live Activities have a maximum duration of 8 hours (12 hours with push updates)
- They require iOS 16.1 or later
- Dynamic Island features require iPhone 14 Pro or later
- You can have multiple Live Activities running simultaneously
- Push notifications can update Live Activities remotely using the push token
The system automatically manages Live Activity lifecycle, removing them after they end or expire. Make sure to test on real devices as Live Activities don't work in the simulator's Dynamic Island.