#import “QFNetWork.h"
//重命名定义代码块
typedef void(^CompletionHandle)(NSURLResponse *response,NSError *error,NSData *data);
//定义私有类,实现发起连接请求,下载数据
@interface QFHttpRequest : NSObject
{
//保存资源地址
NSURL *_url;
//保存缓存策略
NSURLRequestCachePolicy _cachePolicy;
//保存超时时间
NSTimeInterval _timeInterVal;
//保存应答包数据
NSURLResponse *_response;
//保存错误信息
NSError *_error;
//保存下载的数据
NSMutableData *_data;
//保存代码块
CompletionHandle _handle;
}
//定义对应属性
@property(nonatomic,retain)NSURL *url;
@property(nonatomic,assign)NSURLRequestCachePolicy cachePolicy;
@property(nonatomic,assign)NSTimeInterval timeInterVal;
@property(nonatomic,copy)CompletionHandle handle;
@property(nonatomic,retain)NSURLResponse *response;
@property(nonatomic,retain)NSError *error;
@property(nonatomic,retain)NSData *data;
//定义开始下载方法
- (void)start;
@end
//私有类方法实现
@implementation QFHttpRequest
- (void)start
{
//初始化对象
_data = [NSMutableData data];
//创建请求对象并设置下载缓存方式
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url cachePolicy:_cachePolicytimeoutInterval:_timeInterVal];
//调用nsurlconnect的方法设置代理对象并开始异步下载
[NSURLConnection connectionWithRequest:request delegate:self];
}
//实现私有类的代理协议方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
_error = error;
//调用代码块将参数传过去处理
_handle(_response,_error,_data);
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_response = response;
//重置保存下载数据的对象
[_data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//将下载的数据保存起来
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//调用代码快处理信息
_handle(_response,_error,_data);
}
@end
//自定义下载类的实现方法
@implementation QFNetWork
/**
* <#Description#>
*
* @param urlStr url如果有中文,则必须转义编码
* @param cachePolity 缓存策略
* @param timeInterVal 超时时间
* @param hander 完成句柄
*/
//实现自定义下载类创建的下载方法(方法一)
+ (void)sendAsynchronousRequestWithUrl:(NSString *)urlStr cachePolicy:(NSURLRequestCachePolicy)cachePolity timeoutInteral:(NSTimeInterval)timeInterVal copletionHander:(void(^)(NSURLResponse *response,NSError *error,NSData*data))hander
{
//实例化私有下载类
QFHttpRequest *httpRequest = [[QFHttpRequest alloc]init];
//如果有中文,则必须经过转义
httpRequest.url = [NSURL URLWithString:urlStr];
httpRequest.cachePolicy = cachePolity;
httpRequest.timeInterVal = timeInterVal;
httpRequest.handle = hander;
//调用方法开始异步下载
[httpRequest start];
}
//实现自定义下载类创建的下载方法(方法二)
+ (void)sendAsynchronousRequestWithUrl:(NSString *)urlStr completionHander:(void(^)(NSURLResponse *response,NSError*error,NSData *data))hander
{
//调用自定义下载类的自身的方法一实现下载功能
[QFNetWork sendAsynchronousRequestWithUrl:urlStr cachePolicy:NSURLRequestReturnCacheDataElseLoadtimeoutInteral:1.5 copletionHander:hander];
}
@end
#import "QFNetWork.h"
- (void)viewDidLoad {
[super viewDidLoad];
//创建一个imageView控件
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(30, 50, 300, 400);
[self.view addSubview:imageView];
//调用自定义下载类的类方法(方法一)传递参数开始下载
[QFNetWorksendAsynchronousRequestWithUrl:@"http://d.hiphotos.baidu.com/image/w%3D310/sign=c7e6e6bef31f3a295ac8d3cfa924bce3/6609c93d70cf3bc7d48f8a8cd200baa1cc112ac6.jpg" cachePolicy:NSURLRequestReturnCacheDataElseLoadtimeoutInteral:30 copletionHander:^(NSURLResponse *response, NSError *error, NSData *data) {
//实现代码块方法处理下载的数据
if (error == nil) {
imageView.image = [UIImage imageWithData:data];
}else{
NSLog(@"error = %@",error);
NSLog(@"response = %@",response);
}
}];
}