WKDownloadDelegate下载失败,错误代码为-3000。

4
使用新的WKDownloadDelegate时,我尝试下载一个文件,第一次下载成功,但当我再次尝试下载同一文件时,WKDownloadDelegate会调用此方法:
func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?)

当我将此内容打印到控制台时:
download(_ download: <WKDownload: 0x7fc58fb2dfd0>, didFailWithError error: The operation couldn’t be completed. (NSURLErrorDomain error -3000.), resumeData: nil

问题是我找不到与此错误(-3000)相关的任何信息,所以希望有任何建议。

这是整个WKDownloadDelegate代码:

    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)
    try? FileManager.default.createDirectory(at: tempDirectoryPath, withIntermediateDirectories: false)
    let url = tempDirectory.appendingPathComponent(suggestedFilename)
    currentDownloadedPreviewItemUrl = url
    completionHandler(url)
}

public func downloadDidFinish(_ download: WKDownload) {
    guard let currentDownloadedPreviewItemUrl = currentDownloadedPreviewItemUrl else {
        print("Download finished but no URL found")
        return
    }

    DispatchQueue.main.async {
        let activityVC = UIActivityViewController(activityItems: [currentDownloadedPreviewItemUrl], applicationActivities: nil)
        activityVC.popoverPresentationController?.sourceView = self.view
        activityVC.popoverPresentationController?.sourceRect = self.view.frame
        activityVC.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
        self.present(activityVC, animated: true, completion: nil)
    }
}

public func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) {
    debugPrint("------------------")
    debugPrint("download(_ download: \(download), didFailWithError error: \(error.localizedDescription), resumeData: \(resumeData)")
    debugPrint("------------------")
}
1个回答

7

我很笨拙,所以我会把问题写下来,以便再次遇到错误-3000的人可以参考。当使用WKDownloadDelegate和该方法时

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

当传递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)
}

请注意该行。
let url = tempDirectoryPath.appendingPathComponent(suggestedFilename)

使用tempDirectoryPath而不是tempDirectory


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接