在IOS开发中我们可以使用NSURLConnection来请求数据,而数据的返回结果是以代理的形式返回,那么我们就有必要了解相关的代理方法。
首先,我们需要知道的是NSURLConnectionDelegate,显而易见,从名称上我们就很容易看出,它是NSURLConnection的代理。然后,在开发中我们并不是实现NSURLConnectionDelegate的代理方法来获取请求得到的数据的,那是怎么回事呢?
原来,NSURLConnectionDelegate只是帮我们抽象了一些共同的方法,主要用于网络请求的认证等。
那我们应该如何接收请求数据呢?
我们会发现还有两个代理有关网络请求:NSURLConnectionDataDelegate 和 NSURLConnectionDownloadDelegate ,通过查看开发文档我们知道,NKAssetDownload类有一个函数
- (NSURLConnection *)downloadWithDelegate:(id < NSURLConnectionDownloadDelegate >)delegate
当我们使用这个方法时,我们就需要遵循NSURLConnectionDownloadDelegate,其他情况下我们就遵循NSURLConnectionDataDelegate,所以,多数情况下我们都是遵循NSURLConnectionDataDelegate协议的。
搞清楚了这一点我们有必要再对NSURLConnectionDataDelegate协议的方法有一个详细的了解。
我们通过一小段代码来了解这些方法的具体作用:
- (IBAction)buttonClick:(id)sender {
NSString *urlString = @"http://www.baidu.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLCache *urlCache = [NSURLCache sharedURLCache];
[urlCache setMemoryCapacity:(1*1024*1024)];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSCachedURLResponse *response = [urlCache cachedResponseForRequest:request];
if (response) {
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
#pragma mark - NSURLConnectionDelegate -
#pragma mark - NSURLConnectionDataDelegate -
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"将接收输出");
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
NSLog(@"即将发生请求");
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"接收数据");
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSLog(@"将缓存输出");
return cachedResponse;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"请求完成");
}
当我点击button之后在控制台的输出如下:
2014-02-25 15:49:09.874 Demo[23010:a0b] 即将发生请求
2014-02-25 15:49:10.151 Demo[23010:a0b] 将接收输出
2014-02-25 15:49:10.152 Demo[23010:a0b] 接收数据
2014-02-25 15:49:10.157 Demo[23010:a0b] 接收数据
2014-02-25 15:49:10.157 Demo[23010:a0b] 接收数据
2014-02-25 15:49:10.158 Demo[23010:a0b] 接收数据
2014-02-25 15:49:10.165 Demo[23010:a0b] 接收数据
2014-02-25 15:49:10.165 Demo[23010:a0b] 接收数据
2014-02-25 15:49:10.165 Demo[23010:a0b] 接收数据
2014-02-25 15:49:10.166 Demo[23010:a0b] 将缓存输出
2014-02-25 15:49:10.167 Demo[23010:a0b] 请求完成
有没有发现什么?
没错,“将接收输出”只打印了一次,而“接收数据”打印了七次,怎么回事呢?
通过查阅文档发现,原来
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
这个方法在每次数据开始接收前调用,而
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connectionDidFinishLoading:(NSURLConnection *)connection