NSURLSession委托和completionHandler [英] NSURLSession delegate and completionHandler

查看:110
本文介绍了NSURLSession委托和completionHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有completionHandler的NSURLSession + NSURLDownloadTask:

I am using NSURLSession + NSURLDownloadTask with completionHandler:

[session downloadTaskWithURL:downloadURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error)

它运作良好。

但是当我想在progressBar中跟踪下载时,我遇到了问题。
我正在尝试使用NSURLSessionDownloadDelegate来跟踪加载。
使用上面的completionHandler它永远不会被调用?!

But when I want to track download in a progressBar I am running into problems. I am trying to use NSURLSessionDownloadDelegate to track loading. With the upper completionHandler it is never called?!

使用:

 NSURLSessionDownloadTask *downloadTask =
[session downloadTaskWithURL:downloadURL];

将致电代表。

是有没有办法使用completionHandler和委托?

或者在使用completionHandler时跟踪下载的任何其他方式?

Is there any way to use completionHandler AND delegates?
Or any other way to track download while using completionHandler?

推荐答案

使用 didWriteData NSURLSessionDownloadDelegate 的方法,你不得不使用 downloadTaskWithURL 的再现没有 completionHandler 然后实现你自己的 URLSession:downloadTask:didFinishDownloadingToURL:来执行你原本应该做的那些动作完成处理程序。

To employ the didWriteData method of the NSURLSessionDownloadDelegate, you sadly have to use the rendition of downloadTaskWithURL without the completionHandler and then implement your own URLSession:downloadTask:didFinishDownloadingToURL: to perform those actions you otherwise would have done in the completion handler.

这有点烦人(特别是因为在会话对象中设置了 NSURLSessionDownloadDelegate ),但它是怎么回事工作。

This is a little annoying (especially since the NSURLSessionDownloadDelegate is set at the session object), but it's how it works.

这篇关于NSURLSession委托和completionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆