'WKWebview turning blank/white randomly

I'm implementing a simple chat application where users can send documents to each other. Whenever a user sends a document, it can be opened via clicking on the link on the message, and that opens a webview which holds the document in it. I achieved this via UIDocumentPickerViewController. However sometimes, say after sending a certain number of documents, it randomly returns a white Web view with the following error (The app doesn't crash, I get this on the console):

[Process] 0x114863818 - [pageProxyID=6, webPageID=7, PID=66199] WebPageProxy::didFailProvisionalLoadForFrame: frameID=3, domain=NSPOSIXErrorDomain, code=1, isMainFrame=1

I unfortunately, I couldn't find anything useful online regarding this.

Here is the code I use to save the documents and showing them in the webview:

    func saveDocument(urlString: String, fileName: String, pathExtension: String) {
    DispatchQueue.main.async {
        guard let url = URL(string: urlString) else {
            return
        }
        let fileData = try? Data.init(contentsOf: url)
        let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
        let fileNameFromUrl = "test-\(fileName).\(pathExtension)"
        let actualPath = resourceDocPath.appendingPathComponent(fileNameFromUrl)
        do {
            try fileData?.write(to: actualPath, options: .atomic)
            //print("file successfully saved!")
        } catch {
            print("file couldn't be saved!")
        }
    }
}

func showSavedDocument(url: String, fileName: String, pathExtension: String) {
    do {
        let docURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        let contents = try FileManager.default.contentsOfDirectory(at: docURL, includingPropertiesForKeys: [.fileResourceTypeKey], options: .skipsHiddenFiles)
        for url in contents {
            if url.description.contains("\(fileName).\(pathExtension)") {
                viewDocument.layer.borderColor = mainColor.cgColor
                viewDocument.layer.borderWidth = 2
                viewDocument.layer.cornerRadius = 15
                viewDocument.clipsToBounds = true
                self.view.addSubview(viewDocument)
                
                let animation = AnimationType.from(direction: .bottom, offset: 300.0)
                viewDocument.animate(animations: [animation] , duration: 0.5)
                
                let targetURL = url
                let request = URLRequest(url: targetURL)
                documentWebView.load(request)
            }
        }
    } catch {
        print("couldn't locate file!!!!")
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source