如何委托 URLSessionDownloadDelegate
How to delegate URLSessionDownloadDelegate
我已经实现了从远程下载文件的代码我想创建进度条但委托不起作用。
你能帮我理解这个问题的问题吗?
非常感谢大家
import UIKit
class ViewController: UIViewController, URLSessionDownloadDelegate{
var downloadSession: URLSession?
override func viewDidLoad() {
super.viewDidLoad()
downloadSession = URLSession(configuration: .default, delegate: self,
delegateQueue: OperationQueue.main)
guard let url = URL(string: "http://filedownload") else { return }
let task = self.downloadSession?.downloadTask(with: url, completionHandler: {
(URL, response, error) -> Void in
})
task?.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite > 0 {
}
}
}
为了调用委托方法,您需要删除完成处理程序:
let task = self.downloadSession?.downloadTask(with: url)
我已经实现了从远程下载文件的代码我想创建进度条但委托不起作用。
你能帮我理解这个问题的问题吗?
非常感谢大家
import UIKit
class ViewController: UIViewController, URLSessionDownloadDelegate{
var downloadSession: URLSession?
override func viewDidLoad() {
super.viewDidLoad()
downloadSession = URLSession(configuration: .default, delegate: self,
delegateQueue: OperationQueue.main)
guard let url = URL(string: "http://filedownload") else { return }
let task = self.downloadSession?.downloadTask(with: url, completionHandler: {
(URL, response, error) -> Void in
})
task?.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite > 0 {
}
}
}
为了调用委托方法,您需要删除完成处理程序:
let task = self.downloadSession?.downloadTask(with: url)