网络请求之异步代理方法
如果想要发送异步请求,除了上一篇介绍的方法,还可以使用代理方法。涉及的协议有NSURLConnectionDataDelegate,NSURLConnectionDelegate
代码示例:
#import "ViewController.h"
@interface ViewController ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate >
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIProgressView*progress;
@property (weak, nonatomic) IBOutlet UIButton *loadButton;
@property (nonatomic, assign) long long currentLength;
@property (nonatomic, assign) long long SumLength;
//写文件句柄
@property (nonatomic, strong) NSFileHandle *writeHandle;
//文件数据
@property (nonatomic, strong) NSMutableData *fileData;
@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, strong) NSString *filePath;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.currentLength = 0;
self.SumLength = 0;
self.progress.progress = 0;
[self setRequest];
[self.loadButton addTarget:self action:@selector(startRequest) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setRequest
{
NSString *URLString = @"http://www.baidu.com/img/bdlogo.png";
NSURL *URL = [NSURL URLWithString:URLString];
self.request =[NSURLRequest requestWithURL:URL];
}
- (void)startRequest
{
//发送请求
[NSURLConnection connectionWithRequest:self.request delegate:self];
//可以先创建实例,后启动
// NSURLConnection *connection =[NSURLConnection connectionWithRequest:self.request delegate:self];
// [connection start];
}
#pragma mark-NSURLConnectionDataDelegate methods (协议中的方法)
//请求成功(即得到服务器响应后调用,此时还没还是返回数据)
- (void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse *)response
{
//设置文件存储路径
NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",cache);
_filePath = [cache stringByAppendingPathComponent:@"BaiduLogo.png"];
NSLog(@"%@",_filePath);
//通过文件管理器在指定路径创建新文件夹
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:_filePath contents:nil attributes:nil];
//创建文件句柄,通过他指定下载到的数据到指定路径中
self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
self.SumLength = response.expectedContentLength;
NSLog(@"%lf",self.progress.progress);
}
//接收数据的时候调用(注意数据是边读取变传递的,而不是从服务器读取好之后一次性传递到本地)
- (void)connection:(NSURLConnection *)connectiondidReceiveData:(NSData *)data
{
self.currentLength += data.length;
self.progress.progress = self.currentLength/self.SumLength;
NSLog(@"接收到的数据长度 =%lu",(unsigned long)data.length);
NSLog(@"接收到的数据进度 =%f",self.progress.progress);
//通过文件句柄把数据存储到指定的路径下
[self.writeHandle seekToEndOfFile];
[self.writeHandle writeData:data];
}
//数据请求结束的时候调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//关闭链接,不再输入数据到文件中
[self.writeHandle closeFile];
self.writeHandle = nil;
self.currentLength = 0;
self.SumLength = 0;
NSData *data = [[NSData alloc]initWithContentsOfFile:_filePath];
UIImage *image = [UIImage imageWithData:data];
[self.imageView setImage:image];
}
#pragma maek -NSURLConnectionDelegate method
//请求响应失败时调用
- (void)connection:(NSURLConnection *)connectiondidFailWithError:(NSError *)error
{
NSLog(@"RequestError!");
}
@end