NSURLSession网络请求

Qvmaacvt 7年前
   <p>1.在iOS9.0之后,以前使用的NSURLConnection过期,苹果推荐使用NSURLSession来替换NSURLConnection完成网路请求相关操作。</p>    <p>NSURLSession的使用非常简单,先根据会话对象创建一个请求Task,然后执行该Task即可。</p>    <p>NSURLSessionTask本身是一个抽象类,在使用的时候,通常是根据具体的需求使用它的几个子类。关系如下:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/cec63943625c8d61d93fe226d12fd3ee.png"></p>    <p style="text-align:center">1.png</p>    <p>2.使用NSURLSession发送GET请求的方法和NSURLConnection类似,整个过程如下:</p>    <p>1)确定请求路径(一般由公司的后台开发人员以接口文档的方式提供),GET请求参数直接跟在URL后面</p>    <p>2)创建请求对象(默认包含了请求头和请求方法【GET】),此步骤可以省略</p>    <p>3)创建会话对象(NSURLSession)</p>    <p>4)根据会话对象创建请求任务(NSURLSessionDataTask)</p>    <p>5)执行Task</p>    <p>6)当得到服务器返回的响应后,解析数据(XML|JSON|HTTP)</p>    <p>第一种Get方法</p>    <pre>  <code class="language-objectivec">//1.确定请求路径      NSURL *url=[NSURL URLWithString:@"http://123.207.175.144/Test1.php?Login=1"];        //2.创建请求对象      //请求对象内部默认已经包含了请求头和请求方法(GET)      NSURLRequest *request=[NSURLRequest requestWithURL:url];        //3.获得会话对象      NSURLSession *session=[NSURLSession sharedSession];        //4.根据会话对象创建一个Task(发送请求)           /*            第一个参数:请求对象            第二个参数:completionHandler回调(请求完成【成功|失败】的回调)                      data:响应体信息(期望的数据)                      response:响应头信息,主要是对服务器端的描述                      error:错误信息,如果请求失败,则error有值            */      NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){                                          if(error==nil){                                            //6.解析服务器返回的数据                                            //说明:(此处返回的数据是JSON格式的,因此使用NSJSONSerialization进行反序列化处理)                                              NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];                                                NSLog(@"%@",dict);                                          }                                      }];      //5.执行任务      [dataTask resume];</code></pre>    <p>第二种Get方法</p>    <pre>  <code class="language-objectivec">//1.确定请求路径      NSURL *url=[NSURL URLWithString:@"http://123.207.175.144/Test1.php?Login=1"];        //2.获得会话对象      NSURLSession *session=[NSURLSession sharedSession];        //3.根据会话对象创建一个Task(发送请求)           /*                    第一个参数:请求路径                    第二个参数:completionHandler回调(请求完成【成功|失败】的回调)                              data:响应体信息(期望的数据)                              response:响应头信息,主要是对服务器端的描述                              error:错误信息,如果请求失败,则error有值                    注意:                       1)该方法内部会自动将请求路径包装成一个请求对象,该请求对象默认包含了请求头信息和请求方法(GET)                       2)如果要发送的是POST请求,则不能使用该方法          */      NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){            //5.解析数据          NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];          NSLog(@"%@",dict);      }];         //4.执行任务      [dataTask resume];</code></pre>    <p>3.使用NSURLSession发送POST请求的方法和NSURLConnection类似,整个过程如下:</p>    <p>1)确定请求路径(一般由公司的后台开发人员以接口文档的方式提供)</p>    <p>2)创建可变的请求对象(因为需要修改),此步骤不可以省略</p>    <p>3)修改请求方法为POST</p>    <p>4)设置请求体,把参数转换为二进制数据并设置请求体</p>    <p>5)创建会话对象(NSURLSession)</p>    <p>6)根据会话对象创建请求任务(NSURLSessionDataTask)</p>    <p>7)执行Task</p>    <p>8)当得到服务器返回的响应后,解析数据(XML|JSON|HTTP)</p>    <p>Post方法</p>    <pre>  <code class="language-objectivec">//1.创建会话对象      NSURLSession *session=[NSURLSession sharedSession];        //2.根据会话对象创建task      NSURL *url=[NSURL URLWithString:@"http://123.207.175.144/Test1.php"];        //3.创建可变的请求对象      NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];        //4.修改请求方法为POST      request.HTTPMethod=@"POST";        //5.设置请求体      request.HTTPBody=[@"Login=1" dataUsingEncoding:NSUTF8StringEncoding];        //6.根据会话对象创建一个Task(发送请求)           /*                    第一个参数:请求对象                    第二个参数:completionHandler回调(请求完成【成功|失败】的回调)                               data:响应体信息(期望的数据)                               response:响应头信息,主要是对服务器端的描述                               error:错误信息,如果请求失败,则error有值            */       NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){            //8.解析数据          NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];          NSLog(@"%@",dict);      }];         //7.执行任务      [dataTask resume];</code></pre>    <p>4.有的时候,我们可能需要监听网络请求的过程(如下载文件需监听文件下载进度),那么就需要用到代理方法。</p>    <p>代理方法</p>    <pre>  <code class="language-objectivec">#import "ViewController.h"    @interface ViewController ()<NSURLSessionDataDelegate>  @property (nonatomic, strong) NSMutableData *responseData;  @end    @implementation ViewController    -(NSMutableData *)responseData   {           if (_responseData == nil) {                   _responseData = [NSMutableData data];               }           return _responseData;  }    - (void)viewDidLoad {      [super viewDidLoad];      // Do any additional setup after loading the view, typically from a nib.       //[self performSelector:@selector(getAndSynchronousMethod)];      [self delegateTest];  }    -(void)delegateTest  {           //1.确定请求路径           NSURL *url = [NSURL URLWithString:@"http://123.207.175.144/Test1.php?Login=1"];             //2.创建请求对象           //请求对象内部默认已经包含了请求头和请求方法(GET)           NSURLRequest *request = [NSURLRequest requestWithURL:url];             //3.获得会话对象,并设置代理           /*              35      第一个参数:会话对象的配置信息defaultSessionConfiguration 表示默认配置              36      第二个参数:谁成为代理,此处为控制器本身即self              37      第三个参数:队列,该队列决定代理方法在哪个线程中调用,可以传主队列|非主队列              38      [NSOperationQueue mainQueue]   主队列:   代理方法在主线程中调用              39      [[NSOperationQueue alloc]init] 非主队列: 代理方法在子线程中调用              40      */           NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];             //4.根据会话对象创建一个Task(发送请求)           NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];             //5.执行任务           [dataTask resume];    }    //1.接收到服务器响应的时候调用该方法   -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler   {           //在该方法中可以得到响应头信息,即response           NSLog(@"didReceiveResponse--%@",[NSThread currentThread]);             //注意:需要使用completionHandler回调告诉系统应该如何处理服务器返回的数据           //默认是取消的           /*                       NSURLSessionResponseCancel = 0,        默认的处理方式,取消                       NSURLSessionResponseAllow = 1,         接收服务器返回的数据                       NSURLSessionResponseBecomeDownload = 2,变成一个下载请求                       NSURLSessionResponseBecomeStream        变成一个流                    */             completionHandler(NSURLSessionResponseAllow);  }    //2.接收到服务器返回数据的时候会调用该方法,如果数据较大那么该方法可能会调用多次   -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data   {           NSLog(@"didReceiveData--%@",[NSThread currentThread]);             //拼接服务器返回的数据           [self.responseData appendData:data];  }    //3.当请求完成(成功|失败)的时候会调用该方法,如果请求失败,则error有值   -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error   {           NSLog(@"didCompleteWithError--%@",[NSThread currentThread]);             if(error == nil)               {                       //解析数据                       NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];                       NSLog(@"%@",dict);                   }  }    @end</code></pre>    <h3>以上运行结果</h3>    <p style="text-align:center"><img src="https://simg.open-open.com/show/39b2df01f4531dad0770dde8cc456229.png"></p>    <p style="text-align:center">2.png</p>    <p>5.如果返回的字符有汉字,可以为NSDictionary提供一个分类,重写系统中的方法。</p>    <pre>  <code class="language-objectivec">#import "NSDictionary+Log.h"    @implementation NSDictionary (Log)  -(NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level   {           //初始化可变字符串           NSMutableString *string = [NSMutableString string];           //拼接开头[           [string appendString:@"["];             //拼接字典中所有的键值对           [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {                   [string appendFormat:@"%@:",key];                   [string appendFormat:@"%@",obj];               }];             //拼接结尾]           [string appendString:@"]"];             return string;  }    @end</code></pre>    <h3>运行结果</h3>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d4821c61621ff4ffb2f826ef2540e6aa.png"></p>    <p style="text-align:center">3.png</p>    <p> </p>    <p>来自:http://www.jianshu.com/p/11907f606aa8</p>    <p> </p>