如何检测 NSURLConnection 何时完成

发布于 2024-11-28 23:31:03 字数 411 浏览 0 评论 0原文

我正在尝试使用 NSURLRequestNSURLConnection 从 Web API 获取少量数据(大约 50 字节)。我很确定下面的代码是正确的,但是我如何实际获取接收到的数据并检测其何时全部下载?

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self];

I'm trying to use NSURLRequest and NSURLConnection to get a small bit of data (around 50 bytes) from a web API. I'm pretty sure the code below is right, but how do I actually get the received data and detect when its all downloaded?

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随梦而飞# 2024-12-05 23:31:03

通过实现NSURLConnectionDelegate,您可以在connection:didReceiveData:方法中获取数据,并且当操作结束时将调用connectionDidFinishLoading:方法。同时,如果操作过程中出现错误,connection:didFailWithError:将会被调用。

By implementing NSURLConnectionDelegate, you can get the data in connection:didReceiveData: method and the connectionDidFinishLoading: method will be called when the the operation is over. Meanwhile , connection:didFailWithError: will be called if there is some error during the operation.

心头的小情儿 2024-12-05 23:31:03

您将自己设置为代理人,连接将随时向您发送呼叫。
例子:
http://developer.apple.com/库/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

you set yourself as the delegate and the connection will send you calls as it goes.
example:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文