Mobile Development 13 min read

iOS Live Activities Complete Implementation Guide with ActivityKit

This guide explains how to configure a Swift‑only target, define ActivityKit attributes, implement lock‑screen and Dynamic Island UI with ActivityConfiguration, manage creation, updates, and dismissal, handle permissions and server‑side push payloads, and work around network‑image restrictions using shared App Group storage.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
iOS Live Activities Complete Implementation Guide with ActivityKit

Apple introduced Live Activities in WWDC22, allowing users to view real-time app updates on the lock screen. Through ActivityKit, developers can customize the Dynamic Island view. This article provides a comprehensive guide to implementing Live Activities in iOS applications.

Scene Limitations:

Maximum duration of 8 hours, forced dismissal after 12 hours

Must be actively created by the app in the foreground

Live Activities cannot use location services or initiate network requests; limited data (max 4KB) can be sent via push notifications

Multiple cards with similar styles are not recommended due to folding behavior

Project Configuration:

For Objective-C and Swift mixed projects, a Swift file needs to be created for bridging since ActivityKit only supports Swift, and the UI must be built with SwiftUI. A separate Target needs to be added for the Live Activity widget, and the Info.plist must include NSSupportsLiveActivities set to YES.

Implementation Flow:

The core widget implementation uses ActivityConfiguration to define both lock screen and Dynamic Island presentations:

struct LiveActivitiesWidget: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: LiveActivitiesAttributes.self) { context in
// Lock screen UI
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {}
DynamicIslandExpandedRegion(.trailing) {}
DynamicIslandExpandedRegion(.center) {}
DynamicIslandExpandedRegion(.bottom) {}
} compactLeading: {} compactTrailing: {} minimal: {}
}
}
}

Data Model:

The Live Activity attributes consist of static and dynamic (ContentState) properties:

struct ActivityWidgetAttributes: ActivityAttributes {
public struct ContentState: Codable, Hashable {
var process: Double
var title: String
}
var icon: String
}

Main Program Operations:

1. Creation: Use Activity.request() with attributes and initial content state. For push-based updates, pass pushType: .token and monitor pushTokenUpdates asynchronously.

2. Update: Use activity.update(using: contentState) method. iOS 17.2+ supports AlertConfiguration for notification customization.

3. Ending: Three dismissal policies are available: default (4 hours), immediate, or after(specific date).

4. Status Monitoring: Use activityStateUpdates for state changes, contentUpdates for data updates, and pushTokenUpdates for token refreshes. Note: The callback accuracy is approximately 93.33%.

5. Permissions: Check ActivityAuthorizationInfo().areActivitiesEnabled before creating Live Activities.

Server-Side Implementation:

Push notifications require TEAM_ID, AUTH_KEY_ID, TOPIC (bundleIdentifier.push-type.liveactivity), and DEVICE_TOKEN. The APS payload includes timestamp, event (update/end), content-state matching the app's data structure, and optional alert configuration.

Technical Challenge - Network Images:

Since Live Activities cannot make network requests, images must be pre-downloaded in the main app and stored in App Group shared storage, then loaded in the Target widget:

private func downloadImage(from url: URL) async throws -> URL? {
guard var destination = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: "group.com.test.liveactivityappgroup")
else { return nil }
destination = destination.appendingPathComponent(url.lastPathComponent)
// Download and store logic
}
mobile developmentiOSpush notificationsWidgetSwiftUIActivityKitDynamic IslandLive Activities
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.