See docs on: Respond to User Interactions
When users interact with your widget, the system launches your app to handle the request. When the system activates your app navigates to the details that correspond to the widget’s content. Your widget can specify a URL to inform the app what content to display. To configure custom URLs in your widget:
- For all widgets, add the
widgetURL(_:)
view modifier to a view in your widget’s view hierarchy. If the widget’s view hierarchy includes more than one widgetURL
modifier, the behavior is undefined.
- For widgets that use
WidgetFamily.systemMedium
or WidgetFamily.systemLarge
, add one or more Link controls to your widget’s view hierarchy. You can use both widgetURL
and Link
controls. If the interaction targets a Link
control, the system uses the URL in that control. For interactions anywhere else in the widget, the system uses the URL
specified in the widgetURL
view modifier.
For example, a widget that displays details of a single character in a game can use widgetURL
to open the app to that character’s detail.
@ViewBuilder
var body: some View {
ZStack {
AvatarView(entry.character)
.widgetURL(entry.character.url)
.foregroundColor(.white)
}
.background(Color.gameBackground)
}
If the widget displays a list of characters, each item in the list can be in a Link
control. Each Link control specifies the URL
for the specific character it displays.
When the widget receives an interaction, the system activates the containing app and passes the URL to onOpenURL(perform:)
, application(_:open:options:)
, or application(_:open:)
, depending on the life cycle your app uses.
If the widget doesn’t use widgetURL
or Link
controls, the system activates the containing app and passes an NSUserActivity
to onContinueUserActivity(_:perform:)
, application(_:continue:restorationHandler:)
, or application(_:continue:restorationHandler:)
. The user activity’s userInfo dictionary contains details about the widget the user interacted with. Use the keys in WidgetCenter.UserInfoKey
to access these values from Swift code. To access the userInfo values from Objective-C, use the keys WGWidgetUserInfoKeyKind
and WGWidgetUserInfoKeyFamily
instead.