|
| 1 | +# ActivityKit AI Reference |
| 2 | + |
| 3 | +> Live Activity and Dynamic Island implementation guide. Read this document to create Live Activities. |
| 4 | +
|
| 5 | +## Overview |
| 6 | + |
| 7 | +ActivityKit is a framework for creating Live Activities that display real-time progress on the Lock Screen and Dynamic Island. |
| 8 | +It's suitable for **ongoing tasks** such as delivery tracking, sports games, and timers. |
| 9 | + |
| 10 | +## Required Imports |
| 11 | + |
| 12 | +```swift |
| 13 | +import ActivityKit |
| 14 | +import WidgetKit |
| 15 | +import SwiftUI |
| 16 | +``` |
| 17 | + |
| 18 | +## Core Components |
| 19 | + |
| 20 | +### 1. ActivityAttributes (Data Model) |
| 21 | + |
| 22 | +```swift |
| 23 | +struct DeliveryAttributes: ActivityAttributes { |
| 24 | + // Static data (set when Activity is created, cannot be changed) |
| 25 | + let orderNumber: String |
| 26 | + let restaurantName: String |
| 27 | + |
| 28 | + // Dynamic data (can be updated) |
| 29 | + struct ContentState: Codable, Hashable { |
| 30 | + let status: DeliveryStatus |
| 31 | + let estimatedArrival: Date |
| 32 | + let driverName: String? |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +enum DeliveryStatus: String, Codable { |
| 37 | + case ordered = "Order Placed" |
| 38 | + case preparing = "Preparing" |
| 39 | + case pickedUp = "Picked Up" |
| 40 | + case delivering = "On the Way" |
| 41 | + case delivered = "Delivered" |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### 2. Live Activity Widget |
| 46 | + |
| 47 | +```swift |
| 48 | +struct DeliveryLiveActivity: Widget { |
| 49 | + var body: some WidgetConfiguration { |
| 50 | + ActivityConfiguration(for: DeliveryAttributes.self) { context in |
| 51 | + // Lock Screen view |
| 52 | + LockScreenView(context: context) |
| 53 | + } dynamicIsland: { context in |
| 54 | + // Dynamic Island view |
| 55 | + DynamicIsland { |
| 56 | + // Expanded (long press) |
| 57 | + DynamicIslandExpandedRegion(.leading) { |
| 58 | + Image(systemName: "bicycle") |
| 59 | + } |
| 60 | + DynamicIslandExpandedRegion(.trailing) { |
| 61 | + Text(context.state.estimatedArrival, style: .timer) |
| 62 | + } |
| 63 | + DynamicIslandExpandedRegion(.center) { |
| 64 | + Text(context.state.status.rawValue) |
| 65 | + } |
| 66 | + DynamicIslandExpandedRegion(.bottom) { |
| 67 | + ProgressView(value: 0.7) |
| 68 | + } |
| 69 | + } compactLeading: { |
| 70 | + // Compact left |
| 71 | + Image(systemName: "bicycle") |
| 72 | + } compactTrailing: { |
| 73 | + // Compact right |
| 74 | + Text(context.state.estimatedArrival, style: .timer) |
| 75 | + } minimal: { |
| 76 | + // Minimal (shown with other Activities) |
| 77 | + Image(systemName: "bicycle") |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### 3. Lock Screen View |
| 85 | + |
| 86 | +```swift |
| 87 | +struct LockScreenView: View { |
| 88 | + let context: ActivityViewContext<DeliveryAttributes> |
| 89 | + |
| 90 | + var body: some View { |
| 91 | + VStack(alignment: .leading, spacing: 12) { |
| 92 | + HStack { |
| 93 | + Image(systemName: "bicycle") |
| 94 | + .foregroundStyle(.blue) |
| 95 | + Text(context.attributes.restaurantName) |
| 96 | + .font(.headline) |
| 97 | + Spacer() |
| 98 | + Text(context.state.estimatedArrival, style: .timer) |
| 99 | + .font(.title2.monospacedDigit()) |
| 100 | + } |
| 101 | + |
| 102 | + Text(context.state.status.rawValue) |
| 103 | + .font(.subheadline) |
| 104 | + .foregroundStyle(.secondary) |
| 105 | + |
| 106 | + ProgressView(value: progressValue) |
| 107 | + .tint(.blue) |
| 108 | + } |
| 109 | + .padding() |
| 110 | + .activityBackgroundTint(.black.opacity(0.8)) |
| 111 | + } |
| 112 | + |
| 113 | + var progressValue: Double { |
| 114 | + switch context.state.status { |
| 115 | + case .ordered: return 0.2 |
| 116 | + case .preparing: return 0.4 |
| 117 | + case .pickedUp: return 0.6 |
| 118 | + case .delivering: return 0.8 |
| 119 | + case .delivered: return 1.0 |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Complete Working Example |
| 126 | + |
| 127 | +```swift |
| 128 | +import ActivityKit |
| 129 | +import SwiftUI |
| 130 | + |
| 131 | +// MARK: - Attributes |
| 132 | +struct DeliveryAttributes: ActivityAttributes { |
| 133 | + let orderNumber: String |
| 134 | + let restaurantName: String |
| 135 | + |
| 136 | + struct ContentState: Codable, Hashable { |
| 137 | + let status: String |
| 138 | + let remainingMinutes: Int |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// MARK: - Start Live Activity |
| 143 | +func startDeliveryActivity() { |
| 144 | + // Check if supported |
| 145 | + guard ActivityAuthorizationInfo().areActivitiesEnabled else { |
| 146 | + print("Live Activity disabled") |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + let attributes = DeliveryAttributes( |
| 151 | + orderNumber: "12345", |
| 152 | + restaurantName: "Delicious Pizza" |
| 153 | + ) |
| 154 | + |
| 155 | + let initialState = DeliveryAttributes.ContentState( |
| 156 | + status: "Order Placed", |
| 157 | + remainingMinutes: 30 |
| 158 | + ) |
| 159 | + |
| 160 | + let content = ActivityContent( |
| 161 | + state: initialState, |
| 162 | + staleDate: Calendar.current.date(byAdding: .hour, value: 1, to: Date()) |
| 163 | + ) |
| 164 | + |
| 165 | + do { |
| 166 | + let activity = try Activity.request( |
| 167 | + attributes: attributes, |
| 168 | + content: content, |
| 169 | + pushType: nil // Use .token for push updates |
| 170 | + ) |
| 171 | + print("Activity started: \(activity.id)") |
| 172 | + } catch { |
| 173 | + print("Activity start failed: \(error)") |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +// MARK: - Update Live Activity |
| 178 | +func updateDeliveryActivity(activity: Activity<DeliveryAttributes>, newStatus: String, minutes: Int) async { |
| 179 | + let newState = DeliveryAttributes.ContentState( |
| 180 | + status: newStatus, |
| 181 | + remainingMinutes: minutes |
| 182 | + ) |
| 183 | + |
| 184 | + let content = ActivityContent(state: newState, staleDate: nil) |
| 185 | + await activity.update(content) |
| 186 | +} |
| 187 | + |
| 188 | +// MARK: - End Live Activity |
| 189 | +func endDeliveryActivity(activity: Activity<DeliveryAttributes>) async { |
| 190 | + let finalState = DeliveryAttributes.ContentState( |
| 191 | + status: "Delivered", |
| 192 | + remainingMinutes: 0 |
| 193 | + ) |
| 194 | + |
| 195 | + let content = ActivityContent(state: finalState, staleDate: nil) |
| 196 | + |
| 197 | + await activity.end( |
| 198 | + content, |
| 199 | + dismissalPolicy: .default // Dismisses immediately. Can use .after(Date()) |
| 200 | + ) |
| 201 | +} |
| 202 | + |
| 203 | +// MARK: - Get All Activities |
| 204 | +func getAllActivities() -> [Activity<DeliveryAttributes>] { |
| 205 | + return Activity<DeliveryAttributes>.activities |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +## Dynamic Island Layout |
| 210 | + |
| 211 | +### Compact (Default State) |
| 212 | + |
| 213 | +```swift |
| 214 | +compactLeading: { |
| 215 | + // Left: Icon |
| 216 | + Image(systemName: "bicycle") |
| 217 | + .foregroundStyle(.blue) |
| 218 | +} compactTrailing: { |
| 219 | + // Right: Key information |
| 220 | + Text("12 min") |
| 221 | + .font(.caption.monospacedDigit()) |
| 222 | +} |
| 223 | +``` |
| 224 | + |
| 225 | +### Minimal (Shared with Other Activities) |
| 226 | + |
| 227 | +```swift |
| 228 | +minimal: { |
| 229 | + // Small circular area |
| 230 | + Image(systemName: "bicycle") |
| 231 | + .foregroundStyle(.blue) |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +### Expanded (Long Press) |
| 236 | + |
| 237 | +```swift |
| 238 | +DynamicIsland { |
| 239 | + DynamicIslandExpandedRegion(.leading) { |
| 240 | + VStack(alignment: .leading) { |
| 241 | + Image(systemName: "bicycle") |
| 242 | + .font(.title) |
| 243 | + Text("On the Way") |
| 244 | + .font(.caption) |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + DynamicIslandExpandedRegion(.trailing) { |
| 249 | + VStack(alignment: .trailing) { |
| 250 | + Text("12 min") |
| 251 | + .font(.title2) |
| 252 | + Text("ETA") |
| 253 | + .font(.caption) |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + DynamicIslandExpandedRegion(.center) { |
| 258 | + Text("Delicious Pizza") |
| 259 | + .font(.headline) |
| 260 | + } |
| 261 | + |
| 262 | + DynamicIslandExpandedRegion(.bottom) { |
| 263 | + // Progress bar, buttons, etc. |
| 264 | + ProgressView(value: 0.7) |
| 265 | + |
| 266 | + // Interactive button (iOS 17+) |
| 267 | + Button(intent: CallDriverIntent()) { |
| 268 | + Label("Call Driver", systemImage: "phone.fill") |
| 269 | + } |
| 270 | + } |
| 271 | +} |
| 272 | +``` |
| 273 | + |
| 274 | +## Info.plist Setup |
| 275 | + |
| 276 | +```xml |
| 277 | +<key>NSSupportsLiveActivities</key> |
| 278 | +<true/> |
| 279 | +<key>NSSupportsLiveActivitiesFrequentUpdates</key> |
| 280 | +<true/> |
| 281 | +``` |
| 282 | + |
| 283 | +## Push Updates |
| 284 | + |
| 285 | +```swift |
| 286 | +// Request push token when starting Activity |
| 287 | +let activity = try Activity.request( |
| 288 | + attributes: attributes, |
| 289 | + content: content, |
| 290 | + pushType: .token |
| 291 | +) |
| 292 | + |
| 293 | +// Receive token |
| 294 | +for await tokenData in activity.pushTokenUpdates { |
| 295 | + let token = tokenData.map { String(format: "%02x", $0) }.joined() |
| 296 | + // Send token to server |
| 297 | +} |
| 298 | +``` |
| 299 | + |
| 300 | +## Important Notes |
| 301 | + |
| 302 | +1. **Time Limit**: Maximum 8 hours active, persists 4 hours after ending |
| 303 | +2. **Widget Extension Required**: Live Activity is implemented in Widget Extension |
| 304 | +3. **Dynamic Island**: Only supported on iPhone 14 Pro and later (Lock Screen works on all devices) |
| 305 | +4. **Update Frequency**: System may throttle updates |
| 306 | +5. **Background**: Can update via push even when app is in background |
| 307 | + |
| 308 | +## File Structure |
| 309 | + |
| 310 | +``` |
| 311 | +MyApp/ |
| 312 | +├── MyApp/ |
| 313 | +│ ├── MyApp.swift |
| 314 | +│ └── ActivityManager.swift # Activity management logic |
| 315 | +└── MyWidgetExtension/ |
| 316 | + ├── MyLiveActivity.swift # Live Activity Widget |
| 317 | + └── DeliveryAttributes.swift # Shared model (shared with app) |
| 318 | +``` |
0 commit comments