zoukankan      html  css  js  c++  java
  • NSURLSessionConfiguration-0202-网络

      1 //
      2 //  ViewController.m
      3 //  02-NSURLSessionConfiguration
      4 //
      5 #import "ViewController.h"
      6 
      7 @interface ViewController ()
      8 
      9 @end
     10 
     11 @implementation ViewController
     12 
     13 - (void)viewDidLoad {
     14     [super viewDidLoad];
     15     // Do any additional setup after loading the view, typically from a nib.
     16     
     17     
     18     //需求:使用NSURLSessionConfiguration来配置Session
     19     /*
     20      网络请求的流程:
     21      1.构造NSURL连接地址
     22      2.构造NSURLRequest请求对象,包含请求头和请求体信息。
     23      3.构造NSURLSessionConfiguration
     24      4.构造NSURLSession会话对象
     25      5.创建请求任务
     26      6.发送网络请求
     27      
     28      */
     29     
     30     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"];
     31     
     32     //设置Configuration对象
     33 //    requestCachePolicy : 设置缓存策略
     34 //    networkServiceType : 设置网络服务的类型:网络流量,网络电话,语音,视频..
     35 //    timeoutIntervalForRequest:设置超时时间
     36 //    HTTPAdditionalHeaders : 设置请求头
     37 //    discretionary : 用于后台请求,会把WiFi和电量的可用性考虑在内
     38 //    allowsCellularAccess : 是否允许使用蜂窝数据
     39     
     40     NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
     41     
     42     config.timeoutIntervalForRequest = 15;
     43     config.allowsCellularAccess = YES;
     44     
     45     //通过Configuration对象来创建session
     46     //通过代理协议方法来监听数据下载的过程
     47     /*
     48      协议继承关系:3遵守2,2遵守1,因此3包含了所有1,2协议中的方法声明
     49      1.NSURLSessionDelegate
     50      2.NSURLSessionTaskDelegate
     51      3.NSURLSessionDataDelegate
     52      */
     53     NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
     54     
     55     //使用代理协议监听数据加载后就不能再使用block来监听。
     56     NSURLSessionTask *task = [session dataTaskWithURL:url];
     57     
     58     [task resume];
     59     
     60     
     61     
     62 }
     63 
     64 #pragma mark - NSURLSessionDelegate
     65 
     66 //接收到响应头时会调用此协议方法
     67 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
     68     
     69     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
     70     
     71     NSLog(@"StatusCode:%li", httpResponse.statusCode);
     72     
     73     NSLog(@"%@", httpResponse);
     74     
     75     
     76     //可以继续加载,必须允许后才能加载响应体数据
     77     completionHandler(NSURLSessionResponseAllow);
     78     
     79     
     80     /*NSURLSessionResponseCancel = 0, 相当于[task cancel]                                         NSURLSessionResponseAllow = 1  继续正常传输 NSURLSessionResponseBecomeDownload = 2, 把dataTask转成downLoadTask                                 NSURLSessionResponseBecomeStream =3 转成streamTask
     81     */
     82     
     83     
     84 }
     85 
     86 //响应体中的数据每次接收一个数据包时会调用此协议方法,此协议方法可能会被调用多次。
     87 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
     88     
     89     NSLog(@"data:%li", data.length);
     90     
     91 }
     92 
     93 //此协议方法声明在NSURLSessionTaskDelegate中,当任务加载完成后会调用,数据此时已传输完毕。
     94 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
     95     
     96     NSLog(@"网络请求完成");
     97     
     98 }
     99 
    100 @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    [HNOI 2010]Bus 公交线路
    [HNOI 2010]Planar
    [HNOI 2010]chorus 合唱队
    定时器 @Scheduled定点启动
    mysql后获取时间
    kafka基本原理
    cron定时表达式
    自定义导出
    java指定年月的天数和周数<br>
    Date和Calendar时间操作常用方法及示例
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5433055.html
Copyright © 2011-2022 走看看