-1

I have been hunting everywhere for a solution to my problem, and for some reason I am struggling to find any good resources on creating WebViews in SwiftUI and customizing / setting delegates for it. I am working with a 3rd party API that requires me to open a WebView, the user logs into something, and when that's done (signal comes via reaching a certain URL), the sheet should close. At the same time, if a certain click is taken, a new tab would open up, but my WebView is blocking that so no action occurs. These types of links should open in Safari.

I have defined a very simple WebView: UIViewRepresentable already:

struct WebView: UIViewRepresentable {

    var url: URL

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }

    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

And it is called as such (note this is an example):

                        Button {
                            showModal.toggle()
                        } label: {
                            Text("**Check ESPN**")
                        }
                        .sheet(isPresented: $showModal
                               , onDismiss: {}
                               , content: {
                                WebView(url: URL(string: "https://www.espn.com")!)
                            }
                        )

I have found this StackOverflow question that appears to have the delegate I need to open a link in Safari when it attempts to open a new tab. Open a WKWebview target="_blank" link in Safari

On top of this, I need a delegate to watch the URL and when the url contains "done", then the sheet should close.

Where in the world do I put that delegate definition? How do I assign it as a delegate for my struct WebView? Do I need to call my WebView in a different way within my sheet once I add the delegate?

J Smooth
  • 29
  • 1
  • 4
  • Read this tutorial: https://medium.com/@mdyamin/swiftui-mastering-webview-5790e686833e - it has all the details you need – timbre timbre Sep 01 '22 at 15:49

1 Answers1

1

You need to register a WKNavigationDelegate with the WKWebKit instance.
Apple Developer documentation

There is a finished loading callback. Check the URL in the instance. If the loading callback doesn't work, check the url periodically. (Every few seconds or so)

But I see some potential issues with the example code.

Keep the reference of the WKWebKit instance in your type. If you just need to close, then set your type as WKNavigationDelegate.

But probably you need pass the delegate from outside. SwiftUI has to rebuild the UI, means somehow has it to know if the url was reached.

In this case you have to switch to a reference type for your WebView. E.g. making sure you keep talking to the same instance, instead of a copy.

Helge Becker
  • 3,219
  • 1
  • 20
  • 33
  • This is a lot of details that are a little over my head unfortunately. I was hoping to get some implementation details. I already know how to define the WebView and call it, but I have no idea where to define the delegate. Plus, with the delegate, do I need to declare my WebView in a different way as you mentioned? – J Smooth Sep 01 '22 at 15:07