NSURLSessionConfiguration-02-网络
1 // 2 // ViewController.m 3 // 02-NSURLSessionConfiguration 4 // 5 // Created by kangkathy on 15/11/25. 6 // Copyright © 2015年 kangkathy. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 // Do any additional setup after loading the view, typically from a nib. 20 21 22 //需求:使用NSURLSessionConfiguration来配置Session 23 /* 24 网络请求的流程: 25 1.构造NSURL连接地址 26 2.构造NSURLRequest请求对象,包含请求头和请求体信息。 27 3.构造NSURLSessionConfiguration 28 4.构造NSURLSession会话对象 29 5.创建请求任务 30 6.发送网络请求 31 32 */ 33 34 NSURL *url = [NSURL URLWithString:@"http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000"]; 35 36 //设置Configuration对象 37 // requestCachePolicy : 设置缓存策略 38 // networkServiceType : 设置网络服务的类型:网络流量,网络电话,语音,视频.. 39 // timeoutIntervalForRequest:设置超时时间 40 // HTTPAdditionalHeaders : 设置请求头 41 // discretionary : 用于后台请求,会把WiFi和电量的可用性考虑在内 42 // allowsCellularAccess : 是否允许使用蜂窝数据 43 44 NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 45 46 config.timeoutIntervalForRequest = 15; 47 config.allowsCellularAccess = YES; 48 49 //通过Configuration对象来创建session 50 //通过代理协议方法来监听数据下载的过程 51 /* 52 协议继承关系:3遵守2,2遵守1,因此3包含了所有1,2协议中的方法声明 53 1.NSURLSessionDelegate 54 2.NSURLSessionTaskDelegate 55 3.NSURLSessionDataDelegate 56 */ 57 NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 58 59 //使用代理协议监听数据加载后就不能再使用block来监听。 60 NSURLSessionTask *task = [session dataTaskWithURL:url]; 61 62 [task resume]; 63 64 65 66 } 67 68 #pragma mark - NSURLSessionDelegate 69 70 //接收到响应头时会调用此协议方法 71 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { 72 73 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 74 75 NSLog(@"StatusCode:%li", httpResponse.statusCode); 76 77 NSLog(@"%@", httpResponse); 78 79 80 //可以继续加载,必须允许后才能加载响应体数据 81 completionHandler(NSURLSessionResponseAllow); 82 83 84 /*NSURLSessionResponseCancel = 0, 相当于[task cancel] NSURLSessionResponseAllow = 1 继续正常传输 NSURLSessionResponseBecomeDownload = 2, 把dataTask转成downLoadTask NSURLSessionResponseBecomeStream =3 转成streamTask 85 */ 86 87 88 } 89 90 //响应体中的数据每次接收一个数据包时会调用此协议方法,此协议方法可能会被调用多次。 91 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { 92 93 NSLog(@"data:%li", data.length); 94 95 } 96 97 98 //此协议方法声明在NSURLSessionTaskDelegate中,当任务加载完成后会调用,数据此时已传输完毕。 99 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { 100 101 NSLog(@"网络请求完成"); 102 103 } 104 105 106 107 108 @end
时光见证了成长,还很无知,我想一点点幼稚转为有知!