Member-only story
How to Support Multiple Widgets When Using WidgetKit?
This article is originally published at swiftsenpai.com
Having multiple widgets with various functionalities is quite a common feature for apps nowadays. For example, a shopping app can have a widget that shows today’s promotion and another widget that shows the user’s order status.
Apple has given us 2 ways to enable our apps to support multiple widgets:
- Using a widget bundle
- Having multiple widget extensions
In this article, we will look into both of these methods individually, so let’s get started.
Note:
If you are unfamiliar with how to create a widget using WidgetKit, I highly encourage you to first read my article called “Getting Started With WidgetKit” before proceeding.sw
#1: Using a Widget Bundle?
Let’s say we have implemented a widget named MyWidget1
.
@main
struct MyWidget1: Widget {
var body: some WidgetConfiguration {
// Widget configuration here...
// ...
// ...
}
}
Since MyWidget1
has been annotated with the @main
attribute, the system will take that as the widget extension entry point, and show the widget’s snapshot in the widget…