SwiftUI Cant load HTML string WKWebView

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.

Recognized by Mobile Development Collective

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật