NSURLConnection经常使用的代理方法

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

  • NSURLConnectionDelegate:全部类型NSURLConnection的基础代理方法,都是Optional的方法,主要是涉及SSL/TSL加密的相关接口。

@protocol NSURLConnectionDelegate <NSObject>
@optional
//请求错误(失败)的时候调用(请求超时\断网\没有网\。一般指client错误)
- (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;
//当接收到server的响应(连通了server)时会调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
//当接收到server的数据时会调用(可能会被调用多次。每次仅仅传递部分数据)
- (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;
//当server的数据载入完成时就会调用
- (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就不会得到回调,即使你实现了它。道理非常easy。这两类代理一个是用于将下载数据保存到文件上,还有一个是保存到内存中,仅仅能两者居其一。

  • 故事还没有结束 ,假设你实现了connectionDidFinishDownloading:destinationURL而且想通过回到得到的destinationURL读取保存数据的文件时。令人吃惊的发现这个文件竟然不存在,由于这类NSURLConnectionDataDelegate回调是用于Newsstand类型的App开发的,用于将杂志等信息保存到本地文件。

    实在想不通为什么仅仅有Newsstand类型App才干用这组接口,非常多开发人员早已发了bug报告给Apple,Apple也已经确认,可是从iOS5到了iOS7。这个“bug”还是没有被修复。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值