how to make work WKWebView and UIViewRepresentable in swiftui on some pages?

105 views Asked by At

I'm trying to achieve a working WKWebView in SwiftUI, but have some issues, some on navigation not present for an android developer colleague:

  1. I cannot disable/enable back and forward buttons if I am at the first / last page on web navigation.
  2. when using keyboard on some web page with more than one textfield, I.E. a login, I cannot have the default arrows used for jumping between the fields working. Is it my fault or the way safari engine reads the page? can I do something about it?
  3. again on some pages I cannot select with finger the second field unless tap "done" and only after that select the field.
  4. not sure, but is it required while browsing that my browsing buttons view remains visibile while digging?

in the image you can see I'm on second field, but tapping on the "jump up section" notting happens" (same if on the first field" current issue

import SwiftUI

struct ContentView: View {
    @State private var errorMessage: String = "none"
    @State private var canGoBack = false
    @State private var canGoForward = false
    
    var body: some View {
        VStack {
            if let url = URL(string: "https://google.it") {
                
                let customWebView = PreferredAppsCustomWebView(url: url, messageErrorFromWebView: $errorMessage, canGoBack: $canGoBack, canGoForward: $canGoForward)
                customWebView
                
                HStack {
                    Button {
                        customWebView.goBack()
                        let _ = print(customWebView.canGoBack)
                    } label: {
                        Image(systemName: "chevron.left")
                    }
                    //                        .disabled(!canGoBack)
                    .padding(.trailing)

                    Button {
                        customWebView.goForward()
                        let _ = print(customWebView.goForward)
                    } label: {
                        Image(systemName: "chevron.right")
                    }
                    .padding(.leading)
                    //                        .disabled(!canGoForward)

                    Spacer()
                    Button(action: {
//                        selectedItemForWebView = nil //Used to dismiss the webview
                        Logger.info("here dismiss the view")
                    }) {
                        Label("", systemImage: "xmark")
                    }
                }
                .padding()
                .background(Color.gray.opacity(0.2))
            } else {
                var _ = Logger.error("issue with web address")
            }
        }
        .padding()
    }
}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

import SwiftUI
import WebKit

struct PreferredAppsCustomWebView: UIViewRepresentable {
    
    let url: URL
    
    @Binding var messageErrorFromWebView: String
    
    @Binding var canGoBack: Bool
    @Binding var canGoForward: Bool
    
    var webView = WKWebView()
    
    
    //MARK: - UIViewRepresentable
    func makeUIView(context: Context) -> WKWebView {
        webView.navigationDelegate = context.coordinator
        return webView //WKWebView()
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        Logger.info("loading url: \(url)")
        uiView.load(request)
    }
    
    
    //MARK: - Delegate methods section
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, WKNavigationDelegate {
        
        var parent: PreferredAppsCustomWebView
        
        init(_ parent: PreferredAppsCustomWebView) {
            self.parent = parent
        }
        
        func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        }
        
        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
            
            DispatchQueue.main.async {
                self.parent.canGoBack = webView.canGoBack
                self.parent.canGoForward = webView.canGoForward
            }

        }
        
        
        func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
            parent.messageErrorFromWebView = error.localizedDescription
            Logger.error(error.localizedDescription)
        }
        
        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        }
        
    }
    
    func goBack() {
        if webView.canGoBack {
            Logger.info("ok")
            webView.goBack()
        } else {
            Logger.info("no")
        }
    }
    
    func goForward() {
        if webView.canGoForward {
            Logger.info("ok")
            webView.goForward()
        } else {
            Logger.info("no")
        }
    }
    
}
0

There are 0 answers