码迷,mamicode.com
首页 > Web开发 > 详细

NSURLConnection

时间:2015-06-10 14:19:14      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:ios   代理   网络   connection   

NSURLConnection的代理Protocol定义有三类:NSURLConnectionDelegate、NSURLConnectionDataDelegate和NSURLConnectionDownloadDelegate。

  • NSURLConnectionDelegate:所有类型NSURLConnection的基础代理方法,都是Optional的方法,主要是涉及SSL/TSL加密的相关接口。
@protocol NSURLConnectionDelegate <NSObject>
@optional
//请求错误(失败)的时候调用(请求超时\断网\没有网\,一般指客户端错误)
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace NS_DEPRECATED(10_6, 10_10, 3_0, 8_0, "Use -connection:willSendRequestForAuthenticationChallenge: instead.");
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge NS_DEPRECATED(10_2, 10_10, 2_0, 8_0, "Use -connection:willSendRequestForAuthenticationChallenge: instead.");
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge NS_DEPRECATED(10_2, 10_10, 2_0, 8_0, "Use -connection:willSendRequestForAuthenticationChallenge: instead.");
@end
  • NSURLConnectionDataDelegate:用于将网络请求的数据存放到内存中(以NSData的形式)的代理方法。所有方法都是Optional的。
@protocol NSURLConnectionDataDelegate <NSURLConnectionDelegate>
@optional
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;
//当接收到服务器的响应(连通了服务器)时会调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
//当接收到服务器的数据时会调用(可能会被调用多次,每次只传递部分数据)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request;
- (void)connection:(NSURLConnection *)connection   didSendBodyData:(NSInteger)bytesWritten
                                                 totalBytesWritten:(NSInteger)totalBytesWritten
                                         totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse;
//当服务器的数据加载完毕时就会调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
@end
  • NSURLConnectionDownloadDelegate:用于将网络资源直接保存到文件中的代理方法,除了connectionDidFinishDownloading:destinationURL:都是Optional的方法。connectionDidFinishDownloading:destinationURL回调可以告知你下载的网络数据最终存放的文件位置,正常都是在iPhone应用沙盒的/tmp目录下。
@protocol NSURLConnectionDownloadDelegate <NSURLConnectionDelegate>
@optional
- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes;
- (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes;

@required
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *) destinationURL;
@end
  • 由于生成NSURLConnectin对象传入delegate参数时类型就是id,而不是传统id<*Delegate>形式,那么如何确定当前代理实现的是什么类型的NSURLConnectionDelegate代理呢?方法也很诡异,如果你的代理实现了connectionDidFinishDownloading:destinationURL:,那么就表示你要实现的是NSURLConnectionDownloadDelegate,NSURLConnectionDataDelegate中的connection:DidReceiveData就不会得到回调,即使你实现了它。道理很简单,这两类代理一个是用于将下载数据保存到文件上,另一个是保存到内存中,只能两者居其一。

  • 故事还没有结束 ,如果你实现了connectionDidFinishDownloading:destinationURL并且想通过回到得到的destinationURL读取保存数据的文件时,令人惊讶的发现这个文件居然不存在,因为这类NSURLConnectionDataDelegate回调是用于Newsstand类型的App开发的,用于将杂志等信息保存到本地文件。实在想不通为什么只有Newsstand类型App才能用这组接口,很多开发者早已发了bug报告给Apple,Apple也已经确认,但是从iOS5到了iOS7,这个“bug”还是没有被修复。

NSURLConnection

标签:ios   代理   网络   connection   

原文地址:http://blog.csdn.net/fan_yufan/article/details/46440301

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!