ios SwiftUI LiveActivities在启动时抛出异常错误

k2arahey  于 2022-11-19  发布在  iOS
关注(0)|答案(1)|浏览(205)

所以,我尝试着用ActivityKit来创建一个简单的live活动。
编码:
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)
            }
        }
}

它看起来很好,但它根本不工作。
我正在使用一个小部件作为我的主应用程序的新目标。
我在两个Info.plist中都将NSSupportsLiveActivities设置为YES。
按下按钮开始活动后,我得到的所有时间:

有什么建议吗?

ghhaqwfi

ghhaqwfi1#

我找到了解决办法。
您需要将Xcode更新到14.1版本,并重新创建Widget,您会发现在创建阶段出现了“Live Activities”复选框,您可以创建Live Activities。

相关问题