I am trying to load a file from memory and preview it in a WebView in SwiftUI. I already made the same procedure in Swift and it's working, but it's not working in SwiftUI. Files and successfully save to memory and retrieved, but I am getting this error:
WebPageProxy::didFailProvisionalLoadForFrame: frameID=3, domain=NSPOSIXErrorDomain, code=1, isMainFrame=1
This function will return the WebView
private func createURL() -> AnyView{
DocumentManager.shared.save(text: "Hey dear", toDirectory: DocumentManager.shared.documentDirectory(), withFileName: "test345.docx")
let fileURL = DocumentManager.shared.read(fromDocumentsWithFileName: "test345.docx")
return AnyView(Webview(url: URL(fileURLWithPath: fileURL)))
}
This is my UIViewRepresentable:
struct Webview: UIViewRepresentable {
let url: URL
let navigationHelper = WebViewHelper()
func makeUIView(context: UIViewRepresentableContext<Webview>) -> WKWebView {
let webview = WKWebView()
webview.navigationDelegate = navigationHelper
let request = NSURLRequest(url: url as URL) as URLRequest
webview.load(request)
return webview
}
func updateUIView(_ webview: WKWebView, context: UIViewRepresentableContext<Webview>) {
let request = URLRequest(url: self.url, cachePolicy: .returnCacheDataElseLoad)
webview.load(request)
}
}
And this is my WebView helper class:
class WebViewHelper: NSObject, WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("webview didFinishNavigation")
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("didStartProvisionalNavigation")
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("webviewDidCommit")
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
print("didReceiveAuthenticationChallenge")
completionHandler(.performDefaultHandling, nil)
}
}