未调用 URLSessionDataDelegate 方法
URLSessionDataDelegate methods not being called
我正在视图控制器中启动下载会话:
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
,您的委托方法将被调用。
我正在视图控制器中启动下载会话:
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
,您的委托方法将被调用。