-0.4 C
New York
Friday, December 6, 2024

ios – SwiftUI Cant load HTML string WKWebView


We use CloudFlare safety for connection. And when the verification request is triggered, the backend sends us the HTML code, which we open utilizing Webview, respectively, the principle drawback is that I simply see a white display screen on the cellphone. I attempted shifting the code from makeUiView to updateUiView, nevertheless it additionally did not give any end result. I additionally set numerous settings within the .plist file with AllowArbitaryLoadsForWebView (or one thing like that, I do not keep in mind precisely). The identical HTML code opens completely for colleagues on android, however on iOS all I see is only a white display screen. On the identical time, some scripts needs to be executed on this HTML code, however I additionally sort of set all of it up.

I believe that due our privateness coverage sadly I cant share with HTML code, however I checked it on some online-resources and it really works superb.

What I bought – its quite simple code:

WebViewStruct:

struct WebView: UIViewRepresentable {
@Setting(.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(body: .zero, configuration: config)
    webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
    webView.navigationDelegate = delegate
    webView.evaluateJavaScript("navigator.userAgent") { end result, _ in
        if let end result = end result as? String {
            webView.customUserAgent = end result
            CookieWebViewFeature.shared.userAgent = end result
        }
    }

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

And the delegate:

closing 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(.enable)
    }
    
    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("Web page Didn't 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 within the Delegate solely didFinish calling as soon as. All others strategies would not calling.

Idk why this isn’t working. So, in all probability anyone may give me a hand.

The HTML code from backend beginning with:
Only a second....
and the tip:
...ld(cpo);}());

Plus, if I attempting to load one thing easy like :

 ""

its works nice.
Thanks lots.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles