Traditional Culture Encyclopedia - Weather inquiry - iOS 14-WidgetKit

iOS 14-WidgetKit

Widget is like a mini-application, which can quickly access the information it provides-such as weather, calendar events, notes and so on. Widget can also act as a "shortcut", and clicking it will immediately jump to the specified location of the App.

Widget is hosted in the App, so the App function must be implemented first.

You can enjoy the data both online and locally. Local data * * * can be enjoyed through the App group, which is a way of * * * sharing data between apps after iOS 8, and data * * * can be shared only by simple configuration.

After the configuration is completed, data sharing between App and Widget can be realized through UserDefaults or FileManager. Take UserDefaults as an example here, because SwiftUI provides @AppStorage to simplify the operation.

Regardless of the configuration, you need to provide the following.

The data model required to render widgets needs to conform to the TimelineEntry protocol.

Follow the TimelineProvider protocol and tell WidgetKit when to render and refresh widgets. The following two methods need to be implemented:

Placeholder view is a standard SwiftUI view, which will be displayed when it is first displayed or an error occurs.

The content displayed by widgets on the screen needs to be built with SwiftUI, and widgets with different sizes can set different views.

You can only click, and clicking will open the App. You can also configure when passing. WidgetURL(myDeeplink) method can also trigger different deep links by using links.

Small tool box