最近在项目开发中,想实现一个图片下载并展示进度百分比的功能。很自然的,用到了URLSessionDataTask
,通过这个类下载图片,并通过回调方法来观察下载的进度。代码如下:
session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
session?.dataTask(with: url) { (data, response, error) in
print("receive reponse:\(response)")
}.resume()
//回调函数
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Swift.Void) {
print("didReceive response")
}
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
print("didReceive data")
}
然而,发现这两个回调函数根本就没有被调用。发现导致这个原因的问题是上面的代码设置了completionHandler: { (<#Data?#>, <#URLResponse?#>, <#Error?#>)
。只要设置了completionHandler
,回调函数就不执行了。所以上面的代码要改为:
session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
session?.dataTask(with: url).resume()
然而,运行后发现只执行了public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition)
,public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
还是没有执行。网上搜索后发现需要在didReceive response
回调里设置下面这一行,这样didReceive data
方法才会被调用。
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
completionHandler(URLSession.ResponseDisposition.allow)
}
NSURLSessionResponseDisposition
有三个属性,含义如下
NSURLSessionResponseCancel
Cancel the load. Equivalent to calling cancel on the task.
NSURLSessionResponseAllow
Allow the load operation to continue.
NSURLSessionResponseBecomeDownload
Convert this request into a download task.
然而,如果我们不实现didReceive response
,那么didReceive data
方法是会被调用的。