Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKDownloadDelegate download fails with error -3000

When using the new WKDownloadDelegate, I try to download a file, and the first time it downloads fine, but when I attempt to download the same file again, WKDownloadDelegate invokes this method:

func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?)

When I print this to the console:

download(_ download: <WKDownload: 0x7fc58fb2dfd0>, didFailWithError error: The operation couldn’t be completed. (NSURLErrorDomain error -3000.), resumeData: nil

Problem is I can't find any information about this error (-3000), so any suggestions would be nice.

Here is the whole WKDownloadDelegate code

    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("------------------")
}
like image 660
OldTimes Avatar asked Oct 21 '25 17:10

OldTimes


1 Answers

I am clumsy, so I will post what was the problem for anyone that ever encounters error -3000 again. When working with WKDownloadDelegate and the method

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

is invoked, when passing the URL to the completion handler, you MUST ENSURE that a file with the same name doesn't exist, or else it will later throw an error in downloadDidFail method.

So in my case I fixed the code in this method:

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)
}

Notice that line

let url = tempDirectoryPath.appendingPathComponent(suggestedFilename)

is using tempDirectoryPath instead of tempDirectory

like image 108
OldTimes Avatar answered Oct 23 '25 08:10

OldTimes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!