Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh Widget when Main App is used?

I am using an UIKit app and added a widget with SwiftUI and Widgetkit. Now I want to update the widget when the user does something one the main app.

Normally I should use WidgetCenter.shared.reloadAllTimelines() right? But this only works with SwiftUI.

I could say that my widget should reload every second. But my widget does a json request. That would also do the json request every second which I don't want.

For you information. The Main app shares UserDefaults with the widget. Depending on the UserDefaults it reads it and changes it appearance.

Right now the widget loads the correct UserDefault when it is added first. But as I said changing the added Widget doesn't work.

I guess removing and adding the widget is not an userfriendly option.

like image 251
submariner Avatar asked Mar 03 '23 00:03

submariner


1 Answers

WidgetCenter.shared.reloadAllTimelines() is independent from the UI framework you use. You can use it from SwiftUI or UIKit.

If you're worried about making too many requests you could call that method only once your app goes to the background.

like image 127
EmilioPelaez Avatar answered Mar 12 '23 14:03

EmilioPelaez