0

We use CloudFlare protection for connection. And when the verification request is triggered, the backend sends us the HTML code, which we open using Webview, respectively, the main problem is that I just see a white screen on the phone. I tried moving the code from makeUiView to updateUiView, but it also didn't give any result. I also set various settings in the .plist file with AllowArbitaryLoadsForWebView (or something like that, I don't remember exactly). The same HTML code opens perfectly for colleagues on android, but on iOS all I see is just a white screen. At the same time, some scripts should be executed in this HTML code, but I also kind of set it all up.

I think that due our privacy policy unfortunately I cant share with HTML code, but I checked it on some online-resources and it works fine.

What I got - its very simple code:

WebViewStruct:

struct WebView: UIViewRepresentable {
@Environment(\.dismiss) var dismiss
var url: String?
var html: String?
var delegate: WKNavigationDelegate

init(url: String) {
    self.url = url
    self.delegate = WebViewDelegate()
}

init(html: String, successCompletion: @escaping (DismissHandlerState) -> Void) {
    self.html = html
    self.delegate = CookieWebViewDelegate(dismissHandler: successCompletion)
}

then:

    func makeUIView(context: Context) -> WKWebView {
    let config = WKWebViewConfiguration()
    
    config.preferences.javaScriptCanOpenWindowsAutomatically = true
    config.suppressesIncrementalRendering = true
    let webView: WKWebView
    webView = WKWebView(frame: .zero, configuration: config)
    webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
    webView.navigationDelegate = delegate
    webView.evaluateJavaScript("navigator.userAgent") { result, _ in
        if let result = result as? String {
            webView.customUserAgent = result
            CookieWebViewFeature.shared.userAgent = result
        }
    }

    if let url, let rawUrl = URL(string: url) {
        webView.load(URLRequest(url: rawUrl))
    } else if let html {
        webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
    }
    
    return webView
}

And the delegate:

final class CookieWebViewDelegate: NSObject, WKNavigationDelegate {
    let dismissHandler: (DismissHandlerState) -> Void
    
    init(dismissHandler: @escaping (DismissHandlerState) -> Void) {
        self.dismissHandler = dismissHandler
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        decisionHandler(.allow)
    }
    
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        if let url = webView.url?.absoluteString {
            print(url)
        }
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print("Page Failed to load: \(error)")
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        let dataStore = WKWebsiteDataStore.default()
        dataStore.httpCookieStore.getAllCookies { cookies in
            print(cookies)
            if CookieWebViewFeature.shared.checkCookie(cookies) {
                self.dismissHandler(.success)
            } else {
                self.dismissHandler(.failed)
            }
        }
    }
}

Btw in the Delegate only didFinish calling once. All others methods doesn't calling.

Idk why this is not working. So, probably somebody can give me a hand.

The HTML code from backend starting with: <!DOCTYPE html><html lang="en-US"><head><title>Just a moment.... and the end: ...ld(cpo);}());</script></body></html>

Plus, if I trying to load something simple like :

 "<html><body><h1>Hello, World!</h1></body></html>"

its works great. Thanks a lot.

3
  • In the line webView.navigationDelegate = delegate, where is delegate coming from? Commented Sep 5, 2024 at 10:31
  • hi @Sweeper. Thx for ur question, ive update the post with details. Commented Sep 5, 2024 at 10:33
  • 1
    That still begs the question, how did you pass a delegate to the delegate property of WebView? Please show a minimal reproducible example, something that I can copy and paste into a new project and reproduce your issue. Commented Sep 5, 2024 at 10:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.