Im trying to load a specific webpage with the below code. Page loads on the browser on my mac and iphone, Also it works on SFSafariViewController in my app. But WKWebView only shows a white screen. WKWebView can load any other page like google.com, youtube.com etc. How do i troubleshoot this? I do not get any errors either. I need to figure out why I'm only having an issue with this particular web page. Any help would be much appropriated!
@State private var isLoading = true
@State private var error: Error? = nil
let url: URL?
var body: some View {
ZStack {
if let error = error {
Text(error.localizedDescription)
.foregroundColor(.pink)
} else if let url = url {
WebView(url: url,
isLoading: $isLoading,
error: $error)
.edgesIgnoringSafeArea(.all)
if isLoading {
ProgressView()
}
} else {
Text("Sorry, we could not load this url.")
}
}
}
struct WebView: UIViewRepresentable {
let url: URL
@Binding var isLoading: Bool
@Binding var error: Error?
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> WKWebView {
let wkwebView = WKWebView()
wkwebView.navigationDelegate = context.coordinator
wkwebView.load(URLRequest(url: url))
return wkwebView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
}
class Coordinator: NSObject, WKNavigationDelegate {
var parent: WebView
init(_ parent: WebView) {
self.parent = parent
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
parent.isLoading = true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("loading didFinish")
parent.isLoading = false
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("loading error: \(error)")
parent.isLoading = false
parent.error = error
}
}
}
I use to have this issue. For me WebView worked as it should except when I placed the WebView inside a
.sheet().What I did to fix the issue was make the url in WebView
@Binding.I'm not sure why it gives a blank white page. My guess is that it preloads the webpage but since the URL is empty, it loads nothing.