Use WKWebView to open URL and handle button tap using Spotify API

20 views Asked by At

I have been using this project to authenticate with the Spotify API in an iOS app. There is also an example app here. In this example app, it opens the system browser using UIApplication.shared.open(url). I was wondering if instead of doing that a web view could be shown in the app. I do have that working using a WKWebView which looks like this:

struct WebView: UIViewRepresentable {
    let url: URL
    
    class Coordinator: NSObject, WKNavigationDelegate {
        let parent: WebView
        
        init(_ parent: WebView) {
            self.parent = parent
        }
        
        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
            print("Done loading")
        }
    }
    
    func makeCoordinator() -> WebView.Coordinator {
        Coordinator(self)
    }
    
    func makeUIView(context: Context) -> WKWebView {
        let prefs = WKWebpagePreferences()
        prefs.allowsContentJavaScript = true
        let config = WKWebViewConfiguration()
        config.defaultWebpagePreferences = prefs
        return WKWebView(frame: .zero, configuration: config)
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.navigationDelegate = context.coordinator
        let request = URLRequest(url: url)
        uiView.load(request)
    }
}

What is currently happening is when the web view is shown, nothing happens when tapping the "Agree" button. For context this is what the Spotify authorization page looks like:

enter image description here

I guess my question is how can I know when the "Agree" button is tapped? Is there a WKNavigationDelegate function that I should be using?

0

There are 0 answers