Receive sub link from widget in App Delegate

Hi,
I'll try to implement new widgets in my project.

My app is based on objc and swift.
I want to use new "Link" of SwiftUI 2 to receive different URLs from the widget to the AppDelegate of my app.

According with the min 12:40 of this video, I want multiple links in medium and large size of my widget:
developer.apple.com/videos/play/wwdc2020/10028

Like the project "Emoji ranger" from session "Widgets Code-Along".

This is an example of my code:
Code Block
HStack {
            Link(destination: URL(string: "urlschemes://link1")!, label: {
                Image("landscape")
            })
            Link(destination: URL(string: "urlschemes://link2")!, label: {
                Image("portrait")
            })
        }

Where "urlschemes" is the same of URL Schemes in URL types of my app target info.

So I want to receive the selected link on the App Delegate, where I used this method:
Code Block
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSLog(@"RECEIVED URL: %@",url);
}


Thanks in advance
Fulvio

Accepted Reply

Hi,
Thanks for your answer.

Yes the delegate method did not call in AppDelegate.
But I just solved the problem, I found out that I had made a mistake.

in my question I wrote this sample code for use link:
Code Block
Link(destination: URL(string: "urlschemes://link1")!, label: {               
Image("landscape")           
})


But in my project I use a custom view called "myPhotoSwiftUIView" so in the reality my code is :

Code Block
Link(destination: URL(string: "urlschemes://link1")!, label: {               
myPhotoSwiftUIView(imageName: "landscape")           
})

Here is the mistake, inside my custom class I had added the property ".widgetURL", as soon as I removed it the "Link" works correctly, and the App delegate method application:openURL:options:  start to work.

Thanks for your time I hope that my question could help some other developers

Replies

Hi Fulvio,

Thanks for your post, and for including some code snippets.

I wanted to clarify the issue you are seeing. Are you saying that the widget code is working fine, and when you tap on one of the Links in it that it launches your app but that the Objective-C app delegate method application:openURL:options: is not being called?
Hi,
Thanks for your answer.

Yes the delegate method did not call in AppDelegate.
But I just solved the problem, I found out that I had made a mistake.

in my question I wrote this sample code for use link:
Code Block
Link(destination: URL(string: "urlschemes://link1")!, label: {               
Image("landscape")           
})


But in my project I use a custom view called "myPhotoSwiftUIView" so in the reality my code is :

Code Block
Link(destination: URL(string: "urlschemes://link1")!, label: {               
myPhotoSwiftUIView(imageName: "landscape")           
})

Here is the mistake, inside my custom class I had added the property ".widgetURL", as soon as I removed it the "Link" works correctly, and the App delegate method application:openURL:options:  start to work.

Thanks for your time I hope that my question could help some other developers

I have same issue.
I have Widget and want to open specific UiViewController on tap.
BUT link not working for me.
I triger link from Widget but nothing comes in AppDelegate.

Code Block
var body: some View {
    Link(destination: URL(string: "urlschemes://link")!, label: {
        Image("bg0")
     })
}

Help me!