如何使 NSURLConnection 文件下载工作?
我有一个 ViewController 声明为:
@interface DownloadViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
并且我想使用 NSURLConnection 来下载文件。 NSURLConnection 只是“无法启动”,委托方法不起作用(例如,connection:didReceiveResponse 从未被调用)。我注意到在一些示例代码中,该类是 NSObject
的子类,而不是 UIViewController
。
我该如何结合它?我想使用 ViewController 方法,但无法使用 NSURLConnection。
找到一个完整解释如何使用 NSURLConnection 下载文件的示例并不容易。每个人都只关注像didReceiveResponse这样简单的方法。
I have a ViewController declared as:
@interface DownloadViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
and I want to use NSURLConnection to download files. NSURLConnection simply "doesn't start", the delegate methods don't work (for example connection:didReceiveResponse is never called) . I noticed in some sample code that the class was subclassing NSObject
instead of UIViewController
.
How do I combine it? I want to use ViewController methods but then I can't use NSURLConnection.
It's not so easy to find a fully explained example how to download file with NSURLConnection. Everyone only concentrates on the easy methods like didReceiveResponse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 UIViewController 而不是 NSObject 不应该是你的问题!
我在 UIViewController 中使用 NSURLConnection 没有任何问题!
这是我的代码的一部分(不确定它会按原样编译):
-
如果您打算下载大文件,请考虑将接收到的数据包存储在文件中,而不是将其存储在内存中!
Using a UIViewController instead of an NSObject should not be your problem here !
I'm using a NSURLConnection in an UIViewController with no issue !
Here is a part of my code (not sure it will compile as it is) :
-
If you plan to download large files, consider storing the received packets in a file instead of storing it in memory !
如果您遇到问题,可以考虑使用备受推崇的 ASIHTTPRequest 库 来管理您的下载。它会为您照顾一切。
例如,只需 2 行即可完成。
If you're having problems, you could consider using the well regarded ASIHTTPRequest library to manage your download. It takes care of everything for you.
For example, just 2 lines will do it.
使用“NSURLConnection asynchronously”搜索该术语,您将找到源代码。或者只是 NSURLConnection。
例如:
用于异步 Web 服务调用的 NSURLConnection NSURLRequest 代理
<一个href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html" rel="nofollow noreferrer">通过示例代码使用 Apple 的 NSURLConnection
Objective-C 编程教程 – 创建 Twitter 客户端第 1 部分
Use "NSURLConnection asynchronously" search for the term and you'll find source. Or just NSURLConnection.
For example:
NSURLConnection NSURLRequest proxy for asynchronous web service calls
Using NSURLConnection from apple with example code
Objective-C Programming Tutorial – Creating A Twitter Client Part 1