异步下载两个任务(利用协议)


#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>
{
    NSMutableData *csdnData;
    NSMutableData *baiduData;
    NSURLConnection *conn1;
    NSURLConnection *conn2;
}

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSURL *url1 = [NSURL URLWithString:@"http://www.csdn.net"];
    NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
    conn1 = [NSURLConnection connectionWithRequest:request1 delegate:self];
    [conn1 start];
    
    NSURL *url2 = [NSURL URLWithString:@"http://www.ifeng.com"];
    NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
    conn2 = [NSURLConnection connectionWithRequest:request2 delegate:self];
    [conn2 start];
    
    csdnData = [NSMutableData data];
    baiduData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"failed");
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpUrl = (NSHTTPURLResponse *)response;
    if (connection == conn1)
    {
         //状态码为200,说明才有数据
         if (httpUrl.statusCode == 200)
         {
              NSLog(@"Response-------->%p: %@,expectedContentLength=%lld,---%@",connection,httpUrl,response.expectedContentLength,response.URL);//response.expectedContentLength只对于文本,视频等具体的东西它的值才有用,对于网页等动态的是得不到它的具体长度的,得到的也是无意义的。
         }
    }
    else
    {
        //状态码为200,说明才有数据
        if (httpUrl.statusCode == 200)
        {
            NSLog(@"Response-------->%p: %@,expectedContentLength=%lld,---%@",connection,httpUrl,response.expectedContentLength,response.URL);//response.expectedContentLength只对于文本,视频等具体的东西它的值才有用,对于网页等动态的是得不到它的具体长度的
        }
    }

}


//只有-(void)connection: didReceiveData: 才有可能多次调用,因为当文件很大时需要下载多次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
     NSLog(@"Received-------->%p: %d",connection,data.length);
    多个下载需要判断是哪个connection,否则会引起数据污染
    if (connection == conn1) {
        [csdnData appendData:data];
    }
    else
    {
        [baiduData appendData:data];
    }
    
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //多个下载需要判断是哪个connection,否则会引起数据污染
    NSLog(@"finish--------->%p:",connection);
    if (connection == conn1) //通过比较两个指针的地址,来判断是否为同一个NSURLconnection对象
    {
        NSString *str = [[NSString alloc] initWithData:csdnData encoding:NSUTF8StringEncoding];
        NSLog(@"str=%@",str);
    }
    else
    {
        NSString *str1 = [[NSString alloc] initWithData:baiduData encoding:NSUTF8StringEncoding];
        NSLog(@"str1=%@",str1);
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

异步下载步骤:

1,创建NSURL对象

2,创建NSURLRequest对象
3,创建NSURLConnection对象

4,挂出NSURLConnectionDataDelegate协议,并实现协议里的方法。

@protocol NSURLConnectionDataDelegate<NSURLConnectionDelegate>

协议里有四个方法用的最多:

//Fail方法声明在协议NSURLConnectionDelegate里,其他三个在NSURLConnectionDataDelegate里声明

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection

其他
1,response.expectedContentLength只对于文本,视频等具体的东西它的值才有用,对于网页等动态的是得不到它的具体长度的。得到的也是无意义的。
2,if (connection == conn1) //通过比较两个指针的地址,来判断是否为同一个NSURLConnection对象。
3,多个下载需要判断是哪个connection,否则会引起数据污染。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值