I am trying to get a SwiftUI widget to update on command when the value of the @AppStorage changes. When I load the simulator the widget updates to the correct value from @AppStorage but does not update again no matter what I try. To display a new value in the widget, the simulator needs to be closed and reopened.
View in app:
import SwiftUI
import WidgetKit
struct MessageView: View {
@AppStorage("message", store: UserDefaults(suiteName: "group.com.suiteName"))
var widgetMessage: String = ""
var body: some View {
VStack {
Button(action: {
self.widgetMessage = "new message"
WidgetCenter.shared.reloadAllTimelines()
}, label: { Text("button") })
}
}
}
Widget file:
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
@AppStorage("message", store: UserDefaults(suiteName: "group.com.suiteName"))
var widgetMessage: String = ""
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), message: "Have a great day!", configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), message: "Have a great day!", configuration: configuration)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let entry = SimpleEntry(date: Date(), message: widgetMessage, configuration: configuration)
let timeline = Timeline(entries: [entry], policy: .never)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let message: String
let configuration: ConfigurationIntent
}
struct statusWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack{
Text(entry.message)
}
}
}
@main
struct statusWidget: Widget {
let kind: String = "StatusWidget"
var body: some WidgetConfiguration {
IntentConfiguration(
kind: kind,
intent: ConfigurationIntent.self,
provider: Provider()
) { entry in
statusWidgetEntryView(entry: entry)
}
.configurationDisplayName("Note Widget")
.description("Display note from a friend or group")
}
}
question from:
https://stackoverflow.com/questions/65833346/swiftui-widget-not-updating-when-appstorage-value-is-changed 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…