标签:mic conf int response config ken log efault bytes
在.h里面的方法 定义单例类 typedef void(^SuccessBlock) (id block); typedef void(^FailureBlock) (id block); @interface NetRequest : NSObject <NSURLSessionDelegate> + (instancetype)shareInstance; - (void)requestWithURL:(NSString *)url params:(NSDictionary *)params successBlock:(SuccessBlock)success failure:(FailureBlock)failure;
#import <SVProgressHUD.h> // 第三方加载条控件 static NetRequest *reuqest; @implementation NetRequest { SuccessBlock sucblock; FailureBlock faiBlock; NSMutableData *_allData; } + (instancetype)shareInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ reuqest = [[NetRequest alloc] init]; }); return reuqest; } + (instancetype)allocWithZone:(struct _NSZone *)zone { if (!reuqest) { reuqest = [super allocWithZone:zone]; } return reuqest; } - (id)copy { return reuqest; } - (void)requestWithURL:(NSString *)url params:(NSDictionary *)params successBlock:(SuccessBlock)success failure:(FailureBlock)failure { NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15]; NSURLSessionDownloadTask *downLoad =[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]] downloadTaskWithRequest:myRequest]; // 开始请求 [SVProgressHUD showWithStatus:@"正在加载..."]; //提示加载 [downLoad resume]; sucblock = success; faiBlock = failure; } // 请求完成 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSData *data = [NSData dataWithContentsOfURL:location]; id reuslt = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; sucblock(reuslt); [SVProgressHUD dismiss]; } // 网络请求进度 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { double progress = ( double )totalBytesWritten / totalBytesExpectedToWrite; NSLog ( @" 下载进度 ---%f" , progress); [SVProgressHUD showProgress:progress]; }
然后再viewcontroller里面就可以写
// 请求 NSString *string = [NSString stringWithFormat:@"http://……………………………………]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:string]]; // 配置 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; // 任务 NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@", result); }
一般我们都会封装一个AFN的网络请求 可以满足我们基本上的任何需要 但是偶尔我们也是要用一些
NSURLSession 如果特殊情况 封装好了以后可以拿过来用了
标签:mic conf int response config ken log efault bytes
原文地址:http://www.cnblogs.com/wangxiaoqi/p/6440398.html