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