标签:ios 代理 网络 connection
@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
@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
@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”还是没有被修复。
标签:ios 代理 网络 connection
原文地址:http://blog.csdn.net/fan_yufan/article/details/46440301