My Widget loads some data from UserDefaults/ Appgroups
and depending on that it shows some text and a picture.
This works with the first start.
If I change the UserDefaults and use WidgetCenter.shared.reloadAllTimelines()
it only changes the picture not the text.
What can I do? Are there other options to share data between main app and widget? Seems to be a bug.
This is my Widgetentry:
struct Provider: IntentTimelineProvider {
let networkManager = NetworkManager()
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), clubnamehome: "test", clubnameaway: "test2")
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
networkManager.fetchData { (post) in
let entry = SimpleEntry(date: Date(), configuration: configuration, clubnamehome: post.home_name, clubnameaway: post.away_name)
completion(entry)
}
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
networkManager.fetchData { (post) in
let entries = [ SimpleEntry(date: Date(), configuration: configuration, clubnamehome: post.home_name, clubnameaway: post.away_name) ]
let reloadTime = Calendar.current.date(byAdding: .minute, value: 5, to: Date())!
let timeline = Timeline(entries: entries, policy: .after(reloadTime))
completion(timeline)
}
}
}
Here I set the value for UserDefault:
@IBAction func losGehts(_ sender: UIButton) {
switch team {
case "arminia":
UserDefaults(suiteName: "...")!.set("114", forKey: "tabellenId")
if #available(iOS 14.0, *) {
WidgetCenter.shared.reloadAllTimelines()
} else {
// Fallback on earlier versions
}
case "augsburg":
And this my view:
struct MyTeamWidgetEntryView : View {
@Environment(\.colorScheme) var colorScheme
var imageIcon = "stuttgart"
var entry: Provider.Entry
let logo = UserDefaults(suiteName: "...")!.string(forKey: "logo")
let mannschaftsName = UserDefaults(suiteName: "...")!.string(forKey: "teamname")
let teamID = "289"
var body: some View {
HStack {
Spacer()
VStack (alignment: .leading, spacing: 0) {
Spacer().frame(height: 10)
// image von hier
HStack {
Spacer()
switch logo {
case "arminia":
Image("bundesliga1/arminia").resizable().aspectRatio(contentMode: .fit).frame(width: 25, height: 25, alignment: .bottomLeading)
case "augsburg":
Image("bundesliga1/augsburg").resizable().aspectRatio(contentMode: .fit).frame(width: 25, height: 25, alignment: .bottomLeading)
default:
Image("bundesliga1/...").resizable().aspectRatio(contentMode: .fit).frame(width: 90, height: 90, alignment: .center)
}
Spacer()
}
// image bis hier
Spacer().frame(height: 5)
}
Spacer()
VStack (alignment: .leading, spacing: 6) {
VStack {
HStack{
Text(entry.clubnamehome).font(.system(size: 10)).bold()
Text(":").font(.system(size: 10)).bold()
Text(entry.clubnameaway).font(.system(size: 10)).bold()
Spacer()
//überlegen ob spacer rein soll oder nicht
}
}
}
}
}