#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,否则会引起数据污染。