How to update an iOS widget from its parent app?

Viewed 346

Apple's Reminders app has a widget that shows all reminders in a list. When I open the app, add (or remove) a new reminder and close the app again, its widget immediately updates to reflect the changes I just made. I want the same behavior for my app, i.e. I want my app to trigger a live update for its widget.

enter image description here

I understand that widget updates are typically triggered through timelines that I specify in the widget's (Intent)TimelineProvider. So I tried to return a single timeline entry with the current date in order to trigger a live update:

func getTimeline(for configuration: ConfigurationIntent, 
                 in context: Context, 
                 completion: @escaping (Timeline<Entry>) -> ()) {

    let entry = SimpleEntry(date: .now, configuration: configuration)
    let timeline = Timeline(entries: [entry], policy: .atEnd)
    completion(timeline)
}

(I also tried other policies without success.)

Additionally, I call

WidgetCenter.shared.reloadAllTimelines()

in my app whenever the widget needs to update. However, this doesn't work. The widget is not refreshed when I close the app.

What am I doing wrong and how can I trigger a live update for the widget from the parent app?

0 Answers
Related