https://github.com/sparrowcode/live-activity-example
Example how to make, update and end Live Activity. With Dynamic Island and Lock Screen.
https://github.com/sparrowcode/live-activity-example
Last synced: about 2 months ago
JSON representation
Example how to make, update and end Live Activity. With Dynamic Island and Lock Screen.
- Host: GitHub
- URL: https://github.com/sparrowcode/live-activity-example
- Owner: sparrowcode
- License: mit
- Created: 2022-10-21T09:52:30.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-11T15:41:13.000Z (about 1 year ago)
- Last Synced: 2025-04-07T20:21:24.349Z (3 months ago)
- Language: Swift
- Size: 118 KB
- Stars: 30
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Live Activity Example
Example how to make, update and end Live Activity. With Dynamic Island and Lock Screen.
**Full tutorial available at [sparrowcode.io](https://sparrowcode.io/tutorials/live-activities)**.Live Activity already enabled for upload with Xcode 14.1 RC.
> **Note**
> Supported only for iOS >=16.1## Preparing Project
Add Widget target:
Or skip it if already supporting widgets. Next add to `Info.plist` key `Supports Live Activities` to true:
```
NSSupportsLiveActivities```
## Define Model
Define model-data. There is dynamic and static properties. Dynamic can be updated in time and make changes in UI. Static using only for launch Live Activity.
```swift
struct ActivityAttribute: ActivityAttributes {
public struct ContentState: Codable, Hashable {
// Dynamic properties.
// Can be updated when Live Actvity created.
var dynamicStringValue: String
var dynamicIntValue: Int
var dynamicBoolValue: Bool
}
// Static properties.
// Used only when make live actvity.
// Can't update it later.
var staticStringValue: String
var staticIntValue: Int
var staticBoolValue: Bool
}
```## Add UI
Define `Widget` wrapper:
```swift
struct LiveActivityWidget: Widget {
let kind: String = "LiveActivityWidget"
var body: some WidgetConfiguration {
ActivityConfiguration(for: ActivityAttribute.self) { context in
// UI for lock screen
} dynamicIsland: { context in
// UI for Dynamic Island
}
}
}
```