WidgetKit and older iOS versions

Hey,

I am trying to create an iOS App for multiple versions, but i need to refresh my widget with WidgetCenter.shared.reloadAllTimelines() after I added a new data entry inside of my main app.

I dont know why, but this doesnt work for me, (the app crashes) so I tried to use NotificationCenter.
After I realized that NotificationCenter can't communicate between the main app and an extension, I tried to use UserDefaults.

But this doesnt work for me as well.

Pls help me I'm desperate.
Answered by Leopolie in 627402022
Hi,

I already solved my Problem.

WidgetKit should be ignored by older versions, so it shouldnt matter if I am calling
WidgetCenter.shared.reloadAllTimelines() without #available condition.

The problem was, that the WidgetKit framework was not marked as optional BUT NOT the framework inside the widget extension!

You need to add the framework manually to your main app, and than mark this one as optional.
Hi Leopolie, WidgetKit is only available in iOS 14, so you will need to wrap you call to WidgetCenter in an #if check:

Code Block swift
if #available(iOS 14, *) {
WidgetCenter.shared.reloadAllTimelines()
}

Accepted Answer
Hi,

I already solved my Problem.

WidgetKit should be ignored by older versions, so it shouldnt matter if I am calling
WidgetCenter.shared.reloadAllTimelines() without #available condition.

The problem was, that the WidgetKit framework was not marked as optional BUT NOT the framework inside the widget extension!

You need to add the framework manually to your main app, and than mark this one as optional.
Or just check the main app inside Target Membership from the framework and select optional
WidgetKit and older iOS versions
 
 
Q