Skip to content

sparrowcode/live-activity-example

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 

Live Activity Example

Example how to make, update and end Live Activity. With Dynamic Island and Lock Screen.
Full tutorial available at sparrowcode.io.

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:

<key>NSSupportsLiveActivities</key>
<true/>

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.

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:

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
        }
    }
}

About

Example how to make, update and end Live Activity. With Dynamic Island and Lock Screen.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages