'WKWebview not downloading swift

I have been struggling for a while now. My Webview doesn't download files, doesn't matter what kind of file and I don't get anything in the console. Here is my code to handle the download

var filePathDestination: URL?

weak var downloadDelegate: WebDownloadable?

func generateTempFile(with suggestedFileName: String?) -> URL {
   let temporaryDirectoryFolder = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
   return temporaryDirectoryFolder.appendingPathComponent(suggestedFileName ?? ProcessInfo().globallyUniqueString)
}

func downloadFileOldWay(fileURL: URL, optionSessionCookies: [HTTPCookie]?) {
   // Your classic URL Session Data Task
}

func cleanUp() {
   filePathDestination = nil
}

func downloadDidFinish(_ download: WKDownload) {
   
   guard let filePathDestination = filePathDestination else {
       return
   }
   downloadDelegate?.downloadDidFinish(fileResultPath: filePathDestination)
   cleanUp()
}

func download(_ download: WKDownload,
                    didFailWithError error: Error,
                    resumeData: Data?) {
   downloadDelegate?.downloadDidFail(error: error, resumeData: resumeData)
}

func download(_ download: WKDownload, decideDestinationUsing
             response: URLResponse, suggestedFilename: String,
             completionHandler: @escaping (URL?) -> Void) {
   
   filePathDestination = generateTempFile(with: suggestedFilename)
   if(filePathDestination != nil){
       print(filePathDestination!)
   }
   completionHandler(filePathDestination)
   
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
    if navigationAction.shouldPerformDownload {
        decisionHandler(.download, preferences)
    } else {
        decisionHandler(.allow, preferences)
    }
}

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
    if navigationResponse.canShowMIMEType {
        decisionHandler(.allow)
    } else {
        decisionHandler(.download)
    }
}
    
func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) {
    download.delegate = downloadDelegate
}
    
func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) {
    download.delegate = downloadDelegate
}

When I print out navigationAction.request.url I get the right URL, but no download. Any help would be appreciated



Sources

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

Source: Stack Overflow

Solution Source