未调用 URLSessionDelegate 方法
URLSessionDelegate methods not called
首先,我已经检查了类似的现有问题,none 个答案适用。
NSURLSession delegates not called
URLSessionDelegate Function Not Being Called
我正在尝试使用 URLSessionDownloadTask
下载文件,就像这样
class MyNetworkManager : NSObject
{
static let instance = MyNetworkManager()
var downloadSession : URLSession?
init()
{
super.init()
let downloadConfiguration = URLSessionConfiguration.default
downloadSession = URLSession(configuration: downloadConfiguration, delegate: self, delegateQueue: nil)
}
func download(_ url : URL)
{
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "GET"
let downloadTask = downloadSession?.downloadTask(with: urlRequest)
downloadTask?.resume()
}
}
extension MyNetworkManager : URLSessionDelegate
{
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64)
{
//
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL)
{
//
}
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?)
{
//
}
}
但是,没有 URLSessionDelegate
方法被调用。
通常情况下,如果您使用完成处理程序创建任务,则不会调用委托方法 - 事实并非如此,我在创建任务时仅使用 URLRequest
作为参数。
Session 的 delegate
设置正确,调用 downloadTask?.resume()
后其 state
属性 为 running
MyNetworkManager
是单例,我是这样用的
MyNetworkManager.instance.download(someURL)
所以肯定保留了一个实例。
我是不是漏掉了什么?
您必须遵守相关协议,例如:
extension MyNetworkManager: URLSessionDelegate {
// this is intentionally blank
// obviously, if you implement any delegate methods for this protocol, put them here
}
extension MyNetworkManager: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
print(#function)
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
print(#function)
}
}
extension MyNetworkManager: URLSessionTaskDelegate {
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?) {
print(#function, error ?? "No error")
}
}
如果你不符合URLSessionDownloadDelegate
,它不会调用URLSessionDownloadDelegate
方法。
首先,我已经检查了类似的现有问题,none 个答案适用。
NSURLSession delegates not called
URLSessionDelegate Function Not Being Called
我正在尝试使用 URLSessionDownloadTask
下载文件,就像这样
class MyNetworkManager : NSObject
{
static let instance = MyNetworkManager()
var downloadSession : URLSession?
init()
{
super.init()
let downloadConfiguration = URLSessionConfiguration.default
downloadSession = URLSession(configuration: downloadConfiguration, delegate: self, delegateQueue: nil)
}
func download(_ url : URL)
{
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "GET"
let downloadTask = downloadSession?.downloadTask(with: urlRequest)
downloadTask?.resume()
}
}
extension MyNetworkManager : URLSessionDelegate
{
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64)
{
//
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL)
{
//
}
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?)
{
//
}
}
但是,没有 URLSessionDelegate
方法被调用。
通常情况下,如果您使用完成处理程序创建任务,则不会调用委托方法 - 事实并非如此,我在创建任务时仅使用 URLRequest
作为参数。
Session 的 delegate
设置正确,调用 downloadTask?.resume()
后其 state
属性 为 running
MyNetworkManager
是单例,我是这样用的
MyNetworkManager.instance.download(someURL)
所以肯定保留了一个实例。
我是不是漏掉了什么?
您必须遵守相关协议,例如:
extension MyNetworkManager: URLSessionDelegate {
// this is intentionally blank
// obviously, if you implement any delegate methods for this protocol, put them here
}
extension MyNetworkManager: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
print(#function)
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
print(#function)
}
}
extension MyNetworkManager: URLSessionTaskDelegate {
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?) {
print(#function, error ?? "No error")
}
}
如果你不符合URLSessionDownloadDelegate
,它不会调用URLSessionDownloadDelegate
方法。