@main
struct ClockWidgetExt: Widget {
private let kind: String = "ClockWidgetExt"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in
HomeTestView()
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
How can I get data from my Main App to the widget?
The simplest way to send data from a widget to another is by its constructor. Let's say, for example, we need to pass an instance of the above Data class from a screen (PageOne) to another (PageTwo). If you change the data on this page, the updated data is sent to the second page.
For a widget the user frequently views, a daily budget typically includes from 40 to 70 refreshes. This rate roughly translates to widget reloads every 15 to 60 minutes, but it's common for these intervals to vary due to the many factors involved. The system takes a few days to learn the user's behavior.
You can add the AppGroup capability for both your Widget and App (here is a very good explanation how to add it).
Instead of
UserDefaults.standard
just use the shared UserDefaults
for your AppGroup:
UserDefaults(suiteName: <your_app_group>)
Then you can read/write data like explained in this answer.
With the AppGroup entitlement you get access to the shared File Container:
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: <your_app_group>)!
and access an url like this:
let someFileURL = containerURL.appendingPathComponent("SomeFile.txt")
Then you can use your shared File Container like explained in this answer:
You can create a shared CoreData container as well:
let storeURL = containerURL.appendingPathComponent("DataModel.sqlite")
let description = NSPersistentStoreDescription(url: storeURL)
let container = NSPersistentContainer(name: "DataModel")
container.persistentStoreDescriptions = [description]
container.loadPersistentStores { ... }
Then you can use your shared CoreData Container like explained in this answer:
Here is a GitHub repository with different Widget examples including the App Group Widget.
One simple way to do this is by adding both the app and the widget to the same App Group, and then storing and retrieving data from UserDefaults storage located in that App Group's container instead of the default UserDefaults storage for the app. You can access this shared storage for your App Group by initializing UserDefaults using
UserDefaults(suiteName: "YOUR_APP_GROUP_NAME")
instead of accessing it using
UserDefaults.standard
.
This article gives a more thorough description of the details of this process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With