iosswiftswiftuiwidgetkitactivitykit

SwiftUI LiveActivities on start throw weird errors


So, I trying to work with ActivityKit, to create a simple live activity.

Code:

TimerAttributes.swift

import ActivityKit
import SwiftUI

struct TimerAttributes: ActivityAttributes {
    public typealias TimerStatus = ContentState
    
    public struct ContentState: Codable, Hashable {
        var endTime: Date
    }
    
    var timerName: String
}

Widget

import ActivityKit
import WidgetKit
import SwiftUI

struct TimerActivityView: View {
    let context: ActivityViewContext<TimerAttributes>
    
    var body: some View {
        VStack {
            Text(context.attributes.timerName)
                .font(.headline)
            
            Text(context.state.endTime, style: .timer)
        }
        .padding(.horizontal)
    }
}

@main
struct Tutorial_Widget: Widget {
    let kind: String = "Tutorial_Widget"

    var body: some WidgetConfiguration {
        ActivityConfiguration(for: TimerAttributes.self) { context in
            TimerActivityView(context: context)
        } dynamicIsland: { context in
            DynamicIsland {
                DynamicIslandExpandedRegion(.leading) {
                // 2
                }
                DynamicIslandExpandedRegion(.trailing) {
                // 2
                }
                DynamicIslandExpandedRegion(.center) {
                // 2
                }
                DynamicIslandExpandedRegion(.bottom) {
                // 2
                }
            } compactLeading: {
             // 3
            } compactTrailing: {
             // 3
            } minimal: {
             // 4
            }
        }
    }
}

MainView.swift

import ActivityKit
import SwiftUI

struct MainView{
    @State private var activity: Activity<TimerAttributes>? = nil
}

extension MainView: View {
        var body: some View {
            VStack(spacing: 16) {
                Button("Start Activity") {
                    startActivity()
                }
    
                Button("Stop Activity") {
                    stopActivity()
                }
            }
            .buttonStyle(.borderedProminent)
            .controlSize(.large)
        }
    
        func startActivity() {
            let attributes = TimerAttributes(timerName: "Dummy Timer")
            let state = TimerAttributes.TimerStatus(endTime: Date().addingTimeInterval(60 * 5))
    
            do{
                activity = try Activity.request(attributes: attributes, contentState: state)
            }
            catch (let error) {
                print("")
                print("$$$$$$$$$$")
                print(error.localizedDescription)
                print(error)
                print(error.self)
                print("$$$$$$$$$$")
                print("")
            }
        }
    
        func stopActivity() {
            let state = TimerAttributes.TimerStatus(endTime: .now)
    
            Task {
                await activity?.end(using: state, dismissalPolicy: .immediate)
            }
        }
    
        func updateActivity() {
            let state = TimerAttributes.TimerStatus(endTime: Date().addingTimeInterval(60 * 10))
    
            Task {
                await activity?.update(using: state)
            }
        }
}

It's looks fine, but it doesn't work at all.

I'm using a widget as new target to my main App.

I'm set NSSupportsLiveActivities to YES in both Info.plist.

What I get all time after pressing the button to start activity:

Console Output

Any suggestions?


Solution

  • I Found a Solution.

    You need to Update your Xcode to version 14.1, and recreate your widget, you will find out that checkbox 'Live Activities' on create phase appears, and give you a possibility to create Live Activities.