当使用新的 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("------------------")
}
最佳答案
我很笨拙,所以我会为任何再次遇到错误 -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
关于swift - WKDownloadDelegate 下载失败并出现错误 -3000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71926513/