网络请求
2014-02-19 14:03
316 查看
#import <Foundation/Foundation.h> //定义下载中的block typedef void(^ProcessBlock)(NSURLResponse *response,NSData *data,NSError *error); //定义下载完成的block typedef void(^CompletionBlock)(NSURLResponse *response,NSData *data,NSError *error); //接受网络连接协议 @interface CustomConnection : NSURLConnection<NSURLConnectionDataDelegate> + (CustomConnection *)sendAsyRequest:(NSURLRequest *)request andProcessBlock:(ProcessBlock)processblock andCompletionBlock:(CompletionBlock)completionblock; @end
#import "CustomConnection.h" @interface CustomConnection () @property (nonatomic,strong)NSError *error; @property (nonatomic,strong)NSURLResponse *response; @property (nonatomic,strong)NSMutableData *data; @property (nonatomic,strong)CompletionBlock completionBlock;//在这里使用的是strong.引用计数加1(有内存管理) @property (nonatomic,strong)ProcessBlock processBlock; @end @implementation CustomConnection + (CustomConnection *)sendAsyRequest:(NSURLRequest *)request andProcessBlock:(ProcessBlock)processblock andCompletionBlock:(CompletionBlock)completionblock { CustomConnection * connection = [[CustomConnection alloc]initWithRequest:request delegate:nil]; connection.completionBlock = completionblock; connection.processBlock = processblock; [connection start]; return connection; } - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate { //指定当前的网络连接工具类的代理对象是自己 self = [super initWithRequest:request delegate:self]; if (self) { ; } return self; } //收到网络连接响应的时候调用的方法 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { self.response = response; self.data = [[NSMutableData alloc]init]; } ////收到网络链接发送信息时... - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.data appendData:data]; self.processBlock (self.response,self.data,self.error); } //收到网络链接结束信息时... - (void)connectionDidFinishLoading:(NSURLConnection *)connection { self.completionBlock (self.response,self.data,self.error); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { self.error = error; } @end
在使用的时候,
NSURL *url = [NSURL URLWithString:@"http://www.taopic.com/uploads/allimg/110329/23-11032ZK01593.jpg"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [CustomConnection sendAsyRequest:request andProcessBlock:^(NSURLResponse *response, NSData *data, NSError *error) { UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]]; imageV.frame = self.view.bounds; [self.view addSubview:imageV]; NSLog(@"下载中") ; } andCompletionBlock:^(NSURLResponse *response, NSData *data, NSError *error) { NSLog(@"下载完成") ; }];
相关文章推荐
- 一步步搭建Retrofit+RxJava+MVP网络请求框架(一)
- 一步步搭建Retrofit+RxJava+MVP网络请求框架(一)
- React Native网络请求(Frisbee网络框架)
- Http网络请求
- [系统故障]系统无法让您登录,因为发生以下错误:不支持网络请求。
- IOS 网络通信 异步请求
- 实现iOS中发起异步网络请求,需要等待其返回的数据才能执行下一步操作
- IOS 打印网络请求全链接
- Android中Http网络请求库Asnyc-http的使用第三方库
- listview+BaseAdapter + AsyncTask异步请求网络 + LruCache缓存图片
- iOS开发——网络篇——HTTP/NSURLConnection(请求、响应)、http响应状态码大全
- JAVA高级视频 网络编程 05 服务器端接受客户端请求的代码
- iOS 网络开发NSURLConnection——使用block回调方法发送异步请求
- iOS开发之网络请求(基于AFNetworking的再封装)
- IOS学习 网络HTTP Get和Post请求与登录界面加密 涉及按钮边框圆角与颜色设置
- qt之发送http网络请求
- 为自己记----android开发学习记录之网络请求
- Retrofit+ okhttp3 +https的网络请求
- 网络/事务请求去重
- 无限互联学习连载五 网络请求