回头看看NSURLConnection
生活随笔
收集整理的这篇文章主要介绍了
回头看看NSURLConnection
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一、基础代理
1.使用场景:网络错误提示,以及证书使用控制。
2.协议内容@protocol NSURLConnectionDelegate <NSObject>@optional- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;是否使用证书- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;为指定的身份验证发送请求@end
二、数据代理
1.使用场景:用于监测数据的发送和接受,请求状态,缓存控制。
2.协议内容@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
三、下载代理:
1.使用场景:特意为Newsstand下载杂志数据而设立。 使用NKAssetDownload的downloadWithDelegate:(id<NSURLConnectionDownloadDelegate>)delegate方法时使用。
2.协议内容:@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
四、方便发送同步请求的方法
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
五、方便发送异步请求的方法
+ (void)sendAsynchronousRequest:(NSURLRequest*) request queue:(NSOperationQueue*) queue completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler NS_AVAILABLE(10_7, 5_0);
1.使用场景:网络错误提示,以及证书使用控制。
2.协议内容@protocol NSURLConnectionDelegate <NSObject>@optional- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;是否使用证书- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;为指定的身份验证发送请求@end
二、数据代理
1.使用场景:用于监测数据的发送和接受,请求状态,缓存控制。
2.协议内容@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
三、下载代理:
1.使用场景:特意为Newsstand下载杂志数据而设立。 使用NKAssetDownload的downloadWithDelegate:(id<NSURLConnectionDownloadDelegate>)delegate方法时使用。
2.协议内容:@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
四、方便发送同步请求的方法
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
五、方便发送异步请求的方法
+ (void)sendAsynchronousRequest:(NSURLRequest*) request queue:(NSOperationQueue*) queue completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler NS_AVAILABLE(10_7, 5_0);
总结
以上是生活随笔为你收集整理的回头看看NSURLConnection的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: linux将访问日志切成每天,最简单自动
- 下一篇: AssetsLibrary使用介绍