i try to print progress of downloading using URLSessionDownloadDelegate, but delegate's methods don't work Although image is download, progress don't print
I have button
@IBAction func downloadTapped(_ sender: UIButton) {
let image = "https://neilpatel-qvjnwj7eutn3.netdna-ssl.com/wp-content/uploads/2016/02/applelogo.jpg"
guard let url = URL(string: image) else {return}
let operationQueue = OperationQueue()
let session = URLSession(configuration: .default, delegate: self, delegateQueue: operationQueue)
session.downloadTask(with: url) { (data, response, error) in
guard let url = data else {return}
do {
let data = try Data(contentsOf: url)
OperationQueue.main.addOperation {
self.imageView.image = UIImage(data: data)
}
} catch {
}
}.resume()
}
And extension
extension DownloadingViewController: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("=====FINISH=====")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(bytesWritten) / Float(totalBytesWritten)
print(progress)
}
}
nothing at all