回头看看NSURLConnection
生活随笔
收集整理的這篇文章主要介紹了
回头看看NSURLConnection
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、基礎代理
1.使用場景:網絡錯誤提示,以及證書使用控制。
2.協(xié)議內容@protocol NSURLConnectionDelegate <NSObject>@optional- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;是否使用證書- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;為指定的身份驗證發(fā)送請求@end
二、數(shù)據(jù)代理
1.使用場景:用于監(jiān)測數(shù)據(jù)的發(fā)送和接受,請求狀態(tài),緩存控制。
2.協(xié)議內容@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下載雜志數(shù)據(jù)而設立。? 使用NKAssetDownload的downloadWithDelegate:(id<NSURLConnectionDownloadDelegate>)delegate方法時使用。
2.協(xié)議內容:@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
四、方便發(fā)送同步請求的方法
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
五、方便發(fā)送異步請求的方法
+ (void)sendAsynchronousRequest:(NSURLRequest*) request? ? ? ? ? ? ? ? ? ? ? ? ? queue:(NSOperationQueue*) queue? ? ? ? ? ? ? completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler NS_AVAILABLE(10_7, 5_0);
1.使用場景:網絡錯誤提示,以及證書使用控制。
2.協(xié)議內容@protocol NSURLConnectionDelegate <NSObject>@optional- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;是否使用證書- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;為指定的身份驗證發(fā)送請求@end
二、數(shù)據(jù)代理
1.使用場景:用于監(jiān)測數(shù)據(jù)的發(fā)送和接受,請求狀態(tài),緩存控制。
2.協(xié)議內容@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下載雜志數(shù)據(jù)而設立。? 使用NKAssetDownload的downloadWithDelegate:(id<NSURLConnectionDownloadDelegate>)delegate方法時使用。
2.協(xié)議內容:@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
四、方便發(fā)送同步請求的方法
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
五、方便發(fā)送異步請求的方法
+ (void)sendAsynchronousRequest:(NSURLRequest*) request? ? ? ? ? ? ? ? ? ? ? ? ? queue:(NSOperationQueue*) queue? ? ? ? ? ? ? completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler NS_AVAILABLE(10_7, 5_0);
總結
以上是生活随笔為你收集整理的回头看看NSURLConnection的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux将访问日志切成每天,最简单自动
- 下一篇: linux 网卡 巨帧,Linux Ke