How to use .widgetURL?

I have a widget that shows one "event" at a time, so you can have as many copies of the widget as you like, each one showing a different event. This all works fine.

I'm trying to get the main app to open onto a specific page when the user taps a specific widget. For example, the user taps the "Birthday" widget, and the app should show the Birthday page.

In my widget view, I have this on the View object:
Code Block swift
.widgetURL(URL(string: "widget_PreviewEvent:" + event.name))

This sends the text "widget_PreviewEvent:Birthday" to the app. The App's delegate implements the method:
Code Block
application:openURL:options:

I have it logging this out:
Code Block
Delegate: openURL: url = 'widget_PreviewEvent:Birthday'
Main view: showScreen: 'widget_PreviewEvent:Birthday'
Found eventName from url = 'Birthday'

Right, so that works fine. I can tell the app to open the Birthday event, and it does.

However, sometimes, and especially more so on the medium widget, this method is called instead:
Code Block
application:continueUserActivity:restorationHandler:

and the userActivity object is this:
Code Block
userActivity = DynamicEventSelectionIntent, userActivity.userInfo = {
    WGWidgetUserInfoKeyFamily = systemMedium;
    WGWidgetUserInfoKeyKind = "com.company.product.widget"; /* example */
}

"Birthday" isn't mentioned in that object anywhere, so I can't open the Birthday event. How do I fix this?
Answered by darkpaw in 634145022
Right, I have no idea whatsoever if this is the right way of doing it, but it seems to work, so here it is for others who have this issue.

In my MyWidget.swift file, I have this constant and this function:
Code Block swift
let activity: NSUserActivity = NSUserActivity.init(activityType: "ViewEventIntent")
func updateEventToOpenInMainApp(event: EventDetail) -> URL! {
activity.title = "widget_PreviewEvent:"
activity.userInfo = ["widget_PreviewEvent:" : event.name]
activity.becomeCurrent()
return URL(string: "widget_PreviewEvent:" + event.name)
}

In my widget's View:
Code Block swift
struct MyWidgetView: View {
var event: EventDetail /* This object contains the name of the event */
var body: some View {
ZStack {
GeometryReader { g in
Link(destination: updateEventToOpenInMainApp(event: event)) {
HStack {
...
} /* HStack */
.widgetURL(updateEventToOpenInMainApp(event: event))
} /* Link */
} /* GeometryReader */
} /* ZStack */
}
}

It doesn't seem to work properly unless I put both the .widgetURL and the Link() in there ¯\_(ツ)_/¯
Accepted Answer
Right, I have no idea whatsoever if this is the right way of doing it, but it seems to work, so here it is for others who have this issue.

In my MyWidget.swift file, I have this constant and this function:
Code Block swift
let activity: NSUserActivity = NSUserActivity.init(activityType: "ViewEventIntent")
func updateEventToOpenInMainApp(event: EventDetail) -> URL! {
activity.title = "widget_PreviewEvent:"
activity.userInfo = ["widget_PreviewEvent:" : event.name]
activity.becomeCurrent()
return URL(string: "widget_PreviewEvent:" + event.name)
}

In my widget's View:
Code Block swift
struct MyWidgetView: View {
var event: EventDetail /* This object contains the name of the event */
var body: some View {
ZStack {
GeometryReader { g in
Link(destination: updateEventToOpenInMainApp(event: event)) {
HStack {
...
} /* HStack */
.widgetURL(updateEventToOpenInMainApp(event: event))
} /* Link */
} /* GeometryReader */
} /* ZStack */
}
}

It doesn't seem to work properly unless I put both the .widgetURL and the Link() in there ¯\_(ツ)_/¯
How to use .widgetURL?
 
 
Q