IOS开发—网络请求之代理异步请求

网络请求之异步代理方法

如果想要发送异步请求,除了上一篇介绍的方法,还可以使用代理方法。涉及的协议有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

点击加载后界面效果展示:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值