Hands-on the WWDC videos related to iOS widgets.
A widget is a SwiftUI view that updates over time.
Create a widget target:
File > New > Target > Widget.
Activate the target.
We can preview widgets using SwiftUI previews.
Add a preview context:
Widget protocol has a body that is some
WidgetConfiguration. In its body, we can have a static configuration containing modifiers like the configuration display name, the widget’s description, and supported families.
The timeline provider provides snapshots when we want one entry (widget gallery).
PlaceholderView is what the widget should show when it’s not ready.
To preview the placeholder view, do a
Group inside the preview, and both views will be previewed (the placeholder and the actual widget).
There is a SwiftUI modifier
.redacted(reason: .placeholder) that placeholders SwiftUI views very quickly.
Find out how you can integrate system intelligence into your widgets to help them dynamically change at different points during the day and surface the most relevant information. Explore core timeline concepts, support multiple widget families, and learn how to make your widget configurable.
Same concepts as complications’ families.
How do we provide more entries to the timeline when it’s finished?
Pick a policy:
.atEnd creates a new timeline when the current finishes.
.after(date) creates a new timeline when the date arrives.
.never doesn’t create a new timeline when the current finishes.
Other option is to let the widget decide when to update, the system intelligently schedules updates.
Using relevance (optional property in
TimelineEntry) we can give hints to our system to know when to update intelligently our widgets.
SiriKit, just for having configuration our widget is eligible for intelligent system behaviours.
The core technology for configuration is
INIntent, specifically custom intents.
Do not have animation or custom interactions.
But we can link them into our app.
.systemSmall -> 1 tap area
.systemLarge -> can leverage SwiftUI’s
Link to create multiple linkable areas within the Widget.
.widgetURL modifier in the widget view.
.onOpenURL in the SwiftUI perform an action depending on the received URL.
WidgetBundle to have different kinds of widgets in your apps.
Each of this kinds can have implemented all the families.
@main to the widget bundle, and mark its body as
In bigger widget families we can have many clickable areas, we can leverage SwiftUI’s
Learn about the latest improvements to WidgetKit that help power complex complications on watchOS and can help you create Lock Screen widgets for iPhone.
New widget families:
.accessoryCircular: replaces ClockKit’s
.accessoryCorner (Specific to watchOS).
The widget can be full color, accented, or vibrant. This is the
WidgetRenderingMode type, accessible from the Environment.
.fullColor -> Original colors of the widget
.accented -> Two colors, call
.vibrant -> Desaturated, adapting to the grayscale of the content behind it. Use
Making glanceable views
ProgressView for the
ViewThatFits to provides alternatives in case a view is too big for the space available.
.accessoryRectangular, set the title as
To hide sensitive data like the age of the animal ;-), use the modifier