WKDownloadDelegate 下载失败,错误 -3000

Old*_*mes 4 xcode webkit swift

当使用新的 WKDownloadDelegate 时,我尝试下载一个文件,第一次下载正常,但是当我尝试再次下载同一文件时,WKDownloadDelegate 调用此方法:

\n
func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?)\n
Run Code Online (Sandbox Code Playgroud)\n

当我将其打印到控制台时:

\n
download(_ download: <WKDownload: 0x7fc58fb2dfd0>, didFailWithError error: The operation couldn\xe2\x80\x99t be completed. (NSURLErrorDomain error -3000.), resumeData: nil\n
Run Code Online (Sandbox Code Playgroud)\n

问题是我找不到有关此错误(-3000)的任何信息,因此任何建议都会很好。

\n

这是整个 WKDownloadDelegate 代码

\n
    public func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {\n    let tempDirectory = FileManager.default.temporaryDirectory\n    let tempFolderName = UUID().uuidString\n    let tempDirectoryPath = tempDirectory.appendingPathComponent(tempFolderName)\n    try? FileManager.default.createDirectory(at: tempDirectoryPath, withIntermediateDirectories: false)\n    let url = tempDirectory.appendingPathComponent(suggestedFilename)\n    currentDownloadedPreviewItemUrl = url\n    completionHandler(url)\n}\n\npublic func downloadDidFinish(_ download: WKDownload) {\n    guard let currentDownloadedPreviewItemUrl = currentDownloadedPreviewItemUrl else {\n        print("Download finished but no URL found")\n        return\n    }\n\n    DispatchQueue.main.async {\n        let activityVC = UIActivityViewController(activityItems: [currentDownloadedPreviewItemUrl], applicationActivities: nil)\n        activityVC.popoverPresentationController?.sourceView = self.view\n        activityVC.popoverPresentationController?.sourceRect = self.view.frame\n        activityVC.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem\n        self.present(activityVC, animated: true, completion: nil)\n    }\n}\n\npublic func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) {\n    debugPrint("------------------")\n    debugPrint("download(_ download: \\(download), didFailWithError error: \\(error.localizedDescription), resumeData: \\(resumeData)")\n    debugPrint("------------------")\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Old*_*mes 8

我很笨拙,所以我会为再次遇到错误 -3000 的人发布问题是什么。使用 WKDownloadDelegate 和方法时

func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void)
Run Code Online (Sandbox Code Playgroud)

被调用时,将 URL 传递给完成处理程序时,必须确保同名文件不存在,否则稍后会在 downloadDidFail 方法中抛出错误。

因此,就我而言,我修复了此方法中的代码:

public func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {
    let tempDirectory = FileManager.default.temporaryDirectory
    let tempFolderName = UUID().uuidString
    let tempDirectoryPath = tempDirectory.appendingPathComponent(tempFolderName)
    do {
        try FileManager.default.createDirectory(at: tempDirectoryPath, withIntermediateDirectories: false)
    } catch {
        debugPrint(error)
    }
    let url = tempDirectoryPath.appendingPathComponent(suggestedFilename)
    currentDownloadedPreviewItemUrl = url
    completionHandler(url)
}
Run Code Online (Sandbox Code Playgroud)

注意那条线

let url = tempDirectoryPath.appendingPathComponent(suggestedFilename)
Run Code Online (Sandbox Code Playgroud)

使用 tempDirectoryPath 而不是 tempDirectory