我正在视图控制器中启动下载会话:
class MyController: UIViewController {
func startDownload(withURL url: URL) {
downloadSession = URLSession(configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: OperationQueue.main)
downloadTask = downloadSession.dataTask(with: url)
downloadTask.resume()
}
}
委托方法在扩展中定义:
extension MyController: URLSessionDelegate {
func urlSession(_ session: URLSession,
dataTask: URLSessionDataTask,
didReceive data: Data) {
...
}
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?) {
...
}
}
为什么从未调用过这些委托方法?
您只在扩展中声明与URLSessionDelegate
的一致性。如果您声明符合URLSessionDataDelegate
,那么将调用您的委托方法。