NSURLConnection的文件或者图片下载:
//@property (nonatomic, strong) NSMutableData *mutableData;
//得到url
NSURL *url = [NSURL URLWithString:[self.updateContentDict valueForKey:@"commons"]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"GET"]; //设置请求方式
[request setTimeoutInterval:30];//设置超时时间
self.mutableData = [[NSMutableData alloc] init];
[self.view makeToast:@"app下载中..."];
[NSURLConnection connectionWithRequest:request delegate:self];//发送一个异步请求
//Controller须实现NSURLConnectionDataDelegate协议
然后实现以下3个方法
#pragma mark - NSURLConnection delegate
//数据加载过程中调用,获取数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.mutableData appendData:data];
NSLog(@"%@",self.mutableData);
}
//数据加载完成后调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self dismissHUD];
[self.view makeToast:@"下载完成"];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"请求网络失败:%@",error);
}
//以上例子是吧数据放在属性的NSMutableData里面