I'm using an URLSession dataTask to download a file with a URLSessionDownloadDelegate as a result handler. However, urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
is never called. Instead, I get urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
with the error being nil. When using the completionHandler method instead to perform the task, everything works.
Here's my code:
import UIKit
class ViewController: UIViewController, URLSessionDownloadDelegate {
var downloadTask: URLSessionDataTask?
override func viewDidLoad() {
super.viewDidLoad()
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)
let url = URL(string: "https://unsplash.it/200/300/?random")!
//downloadTask = session.dataTask(with: request)
downloadTask = session.dataTask(with: url)
downloadTask!.resume()
}
@IBAction func cancelButtonTapped(_ sender: Any) {
downloadTask?.cancel()
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
print("session: didBecomeInvalidWithError: \(error?.localizedDescription)")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Your data is here!")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten / totalBytesExpectedToWrite)
print("Making progress: \(progress)")
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("session: task: didCompleteWithError: \(error?.localizedDescription)")
session.finishTasksAndInvalidate()
}
}
The simulator output is
session: task: didCompleteWithError: nil
session: didBecomeInvalidWithError: nil
Thank you in advance.
You should use
URLSessionDownloadTask
instead ofURLSessionDataTask
and use background forURLSessionConfiguration
as follows: