【问题标题】:WKWebview not downloading swiftWKWebview 没有快速下载
【发布时间】:2022-08-07 07:33:22
【问题描述】:

我已经挣扎了一段时间了。我的 Webview 不下载文件,无论是哪种文件,我在控制台中什么也得不到。 这是我处理下载的代码

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
}

当我打印出来navigationAction.request.url我得到了正确的 URL,但没有下载。任何帮助,将不胜感激

    标签: ios swift webview wkwebview


    【解决方案1】:

    你有没有最终找到解决方案?我在这里发布了类似的内容,答案中的方法对我不起作用。那对你有用吗?

    How to download files from inside WKWebview on macOS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      相关资源
      最近更新 更多