How do you reload an iOS 14 Widget from an Objective-C app?

Does anyone have complete instructions - including ALL steps and information on everything that needs to be done - and example code that we can use to reload SwiftUI widgets from an Objective-C main app?

It's all well and good telling us to look at a page that gives us generic info on importing Swift header files, but that doesn't really help; it just gives us yet another thing to go searching for actual info on.

There are no classes in my widget code; just structs, extensions and some funcs to figure out dates, so there's nowhere to make anything public or to add "@objc" to.

If it is possible to reload a SwiftUI widget from an Objective-C main app, then Apple should provide us with such code. That would be really simple for them to do. Yeah, widgets are new, which is why the source of such information should be Apple themselves, not from random pages you find on the internet.

These forums don't have anywhere near as many responses from Apple engineers as are required. (I asked five days ago how to format a Text.init(theDate, style: .timer) - no response.)

Yes, this may seem a little like a rant, but why doesn't Apple give us example code for their new features? Not every app has been written in Swift, and for many it would be too costly to convert.
Answered by zchan0 in 632551022

If it is possible to reload a SwiftUI widget from an Objective-C main app

Sure. You can define a Swift class, and put your reload methods there. Dont forget to add @objc attribute to the function.

Code Block Swift
import WidgetKit
class WidgetContentLoader: NSObject {
@objc public static func reloadWidgetContent() {
WidgetCenter.shared.reloadTimelines(ofKind:..)
}
}


Now you can call the function in the Objective-C file.

Code Block objc
[WidgetContentLoader reloadWidgetContent];






Accepted Answer

If it is possible to reload a SwiftUI widget from an Objective-C main app

Sure. You can define a Swift class, and put your reload methods there. Dont forget to add @objc attribute to the function.

Code Block Swift
import WidgetKit
class WidgetContentLoader: NSObject {
@objc public static func reloadWidgetContent() {
WidgetCenter.shared.reloadTimelines(ofKind:..)
}
}


Now you can call the function in the Objective-C file.

Code Block objc
[WidgetContentLoader reloadWidgetContent];






Finally. Thank you!

(Why don't Apple put this on their site anywhere?)
How do you reload an iOS 14 Widget from an Objective-C app?
 
 
Q