iOS开发3年只用5分钟搞定面试官

移动开发 iOS
假舆马者,非利足也,而致千里;假舟楫者,非能水也,而绝江河。君子生非异也,善假于物也。我们曾借白茶清欢等一个人,曾借花开花落叹宠辱不惊。程序(Program)是一个可以运行的文件, 一个程序至少有一个进程,一个进程至少有一个线程,即主线程

序言

假舆马者,非利足也,而致千里;假舟楫者,非能水也,而绝江河。君子生非异也,善假于物也。

我们曾借白茶清欢等一个人,曾借花开花落叹宠辱不惊。

程序(Program)是一个可以运行的文件, 一个程序至少有一个进程,一个进程至少有一个线程,即主线程

正文

  • 程序(Program)是一个可以运行的文件, 一个程序至少有一个进程,一个进程至少有一个线程,即主线程
  • 进程:正在进行的程序被称为进程,负责程序运行的内存分配,每个进程都有自己的独立虚拟内存空间.一个程序的一次运行,在执行过程中拥有独立的内存单元,而多个线程共享一块内存
  • 什么是线程:线程是进程中的基本单元(可执行的代码段),线程可以并发运行,提高执行效率
  • 创建线程的目的:就是为了开启一条新的可执行的代码段,与主线程中的代码实现同时运行,防止界面假死,是实现异步的技术的主要手段,比如网络异步下载

一 Runloop知识点补充

1 在模拟器中拖拽UITextView的时候对RunLoop模式的影响

1.1 performSelector: 方法

—-对该方法的解释: 该方法运行的时候回受到runloop运行模式的影响,默认设置为defaulmode,当拖拽的时候,runloop切换模式,所以不执行

代码块和解释一:

 

  1. //由于该方法设置了RunLoop的运行模式为两种,当用户滑动UITextView的时候RunLoop切换模式并且继续执行,所以能设置出图片 
  2.     [self.imageView1 performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"/Users/xiaofeng/Desktop/Snip20160319_18.png"] afterDelay:5.0 inModes:@[UITrackingRunLoopMode,NSDefaultRunLoopMode]]; 

代码块和解释二:

 

  1. //该方法的执行会受到外界的影响,当用户滑动UITextView的时候,并不会经过2秒设置图片,runloop运行的模式是默认的模式,当用户滑动UITextView的时候,切换了模式,所以不会设置图片 
  2.     [self.imageView1 performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"/Users/xiaofeng/Desktop/Snip20160319_15.png"] afterDelay:2.0]; 

2 问题:怎么能让一个线程一直活着,然后在特定的情况下,让线程跳转任务

二 常驻线程

3 保证线程不死的方法:创建一个RunLoop循环;然后设置数据源或者定时器

  • 3.1 ***种方法:开启一个死循环—>比如while死循环,保持线程不死亡,这样虽然能保证线程不死,但是不能保证线程去执行其它的任务(不可取)
  • 3.2 第二种方法:开启一个RunLoop循环,也可以保证让线程不死,但是开启了需要手动执行,并且需要设置运行模式,否则单单只是开启了RunLoop循环,并没有设置模式的话,RunLoop开启了就直接退出,并不会一直往下执行(开启RunLoop需要完成指定的三个步骤)

4 需求:当点击创建线程的按钮的时候,开始创建一条线程,然后点击让子线程开始干其他的工作的时候,子线程开始执行其他工作

1 创建子线程按钮:

 

  1. #pragma mark - 创建子线程按钮 
  2. - (IBAction)creatBtnClick:(id)sender 
  3.     //创建子线程 
  4.     NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(task) object:nil]; 
  5.       
  6.     [thread start]; 
  7.     //如果不加入这句代码,线程在执行完task方法之后就会立刻被释放,这里写这句代码目的就是保证线程不死,能继续执行其他的任务 
  8.     self.thread = thread; 

创建子线程按钮中的方法实现: 在task方法中我们只要求打印当前线程就行.

让子线程开始继续工作:

 

  1. #pragma mark - 让线程继续工作 
  2.   
  3. - (IBAction)goOnBtnClick:(id)sender 
  4.     [self performSelector:@selector(task2) onThread:self.thread withObject:nil waitUntilDone:YES]; 

问题:如果就这样运行的话,就会报错.

报错原因:虽然上面在创建子线程中已经写了一句self.thread = thread保证子线程不会被释放,但是由于没有开启runloop循环,那么子线程其实是处于死亡状态,所以当在点击让子线程继续工作的话就会报错.

解决办法:创建子线程的RunLoop,让子线程一直在运行,然后通过设置在方法里面的调用其它的需要子线程工作的方法,让子线程去工作

 

  1. #pragma mark - 工作在线程中的任务 
  2. - (void)task 
  3.     NSLog(@"1------%@",[NSThread currentThread]); 
  4.     //创建RunLoop 
  5.     NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 
  6.    
  7.     //创建timer(这种方法需要手动设置模式) 
  8.     NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES]; 
  9. // 
  10. //    //添加到runloop中,并设置模式 
  11.     [runLoop addTimer:timer forMode:NSDefaultRunLoopMode]; 
  12. //    [runLoop addPort:[NSPort port] forMode:NSDefaultRunLoopMode]; 
  13.   
  14.     //开启runloop 
  15.   
  16.     [runLoop run]; 
  17.     NSLog(@"%s-------2-----%@",__func__,[NSThread currentThread]); 

注意:上面创建时钟的代码和添加时钟到runloop中的代码可以写成下面一句,同样也能保证线程不处于死亡状态

  1. [runLoop addPort:[NSPort port] forMode:NSDefaultRunLoopMode]; 

5 RunLoop的自动释放池

  • ***次创建 RunLoop启动的时候
  • ***一次 RunLoop退出的时候
  • 其它时间的创建和销毁:当RunLoop即将休眠的时候会把之前的自动释放池销毁,重新创建一个新的

6 RunLoop在网络中的应用(直接看代码就可以)

 

  1. - (void)delegate1 
  2.     //确定请求路径 
  3.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; 
  4.     //创建可变的请求对象 
  5.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
  6.     //设置代理 
  7.     NSURLConnection *connention = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 
  8.   
  9.     //加入该段代码可以改变代理方法执行的线程,默认是在主线程中执行,但是加入该段代码之后,代理方法会在子线程中执行 
  10.     [connention setDelegateQueue:[[NSOperationQueue alloc] init]]; 
  11.   
  12.     //开始发送请求 
  13.     //1)该方法内部会吧connention对象作为一个source添加到runloop中,并且制定运行模式为默认 
  14.     //2)如果发现当前的runloop不存在,那么该方法内部会自动的创建并开启当前子线程的runloop 
  15.     [connention start]; 

三 网络

1 GET和POST对比:

  • GET请求参数直接跟在URL后面(?)
  • POST请求参数是在请求体里面

2 HTTP基本通信过程:客户端—>请求—->服务器;服务器—>响应—->客户端

 

  1. 具体的操作步骤: 
  2. 2.1 确定请求路径 
  3. 2.2 获取主机名 
  4. 2.3 DNS域名解析 
  5. 2.4 获得端口号 
  6. 2.5 链接到120.25.226.186的端口80 
  7. 2.6 发送一个HTTP GET请求 
  8. 2.7 接收服务器的响应 
  9. 2.8 关闭链接 

3 请求和响应

请求:

  • 请求头:包含了客户端的环境描述,客户端请求信息等
  • 请求体:客户端发给服务器的具体数据,比如文件数据(POST请求才会有)

响应:

  • 响应头:包含了对服务器的描述,对返回数据的描述
  • 响应体:服务器返回给客户端的具体数据,比如文件数据

如图:

 

4 HTTP请求

HTTP请求的第三方框架:ASIHttpRequest(已经弃用);AFNetworking(主用);MKNetworking

苹果自带的:

  • NSURLConnection:用法简单,最古老最经典最直接的一种方案
  • NSURLSession:功能比NSURLConnection更强大,苹果目前比较推荐的使用技术(重要)
  • CFNetwork:NSURL*的底层,纯C语言

第三方框架:(企业开发基本使用的是第三方框架)

  • ASIHttpRequest:外号”HTTP终结者”,功能极其强大,可惜已经停止更新了
  • AFNetworking:简单易用,提供了基本够用的常用功能,维护和使用者多(重要)
  • MKNsetworking:简单易用,产生三哥的故乡印度,维护和使用者少

四 GET方式

  • 概念:发送网络请求的两种方式,主要区别上面已经有写
  • 发送同步请求

具体步骤:

 

  1. 1> 确定请求路径 
  2. 2> 创建请求对象 
  3. 3> 发送请求 
  4. 4 > 解析接收数据 

 

  1. #pragma mark - 发送同步请求 
  2. - (void)sendSync 
  3.     //确定请求路径 
  4.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; 
  5.   
  6.     //创建请求对象 
  7.     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
  8.   
  9.     //初始化响应头信息(设置为空) 
  10.     NSHTTPURLResponse *response = nil; 
  11.     //初始化错误信息 
  12.     NSError *error = nil; 
  13.   
  14.     //发送请求 
  15.     /** 
  16.      *  ***个参数:请求对象 
  17.      * 
  18.      *  第二个参数:响应头信息(传入的是地址) 
  19.      * 
  20.      *  第三个参数:错误信息(如果发送请求失败,那么error就有值)(传入的是地址) 
  21.      */ 
  22.     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
  23.   
  24.     //解析返回的响应数据 
  25.     NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 

3 发送异步请求

注意:同步请求和异步请求的主要区别就是发送请求中的方法不同.

 

  1. #pragma mark - 发送异步请求 
  2.   
  3. - (void)sendAsync 
  4.     //确定请求路径 
  5.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; 
  6.   
  7.     //创建请求对象 
  8.     NSURLRequest *resquest = [NSURLRequest requestWithURL:url]; 
  9.   
  10.     //发送请求 
  11.     /** 
  12.      *  参数一:请求对象 
  13.      * 
  14.      *  参数二:队列(作用在completionHandler上面) 
  15.      * 
  16.      *  参数三:响应的信息(响应头;响应的数据) 
  17.                 response 响应头信息 
  18.                 data     响应体信息 
  19.                 connectionError 错误信息 
  20.      */ 
  21.     [NSURLConnection sendAsynchronousRequest:resquest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 
  22.         //解析数据 
  23.         NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  24.     }]; 

4 用代理的方法发送网络请求

注意:需要遵守协议:

  1. <nsurlconnectiondatadelegate></nsurlconnectiondatadelegate> 

代理方法发送,里面包括了设置代理的三种方式

 

  1. #pragma mark - 代理方法发送请求 
  2. - (void)sendAsyncDelegate 
  3.     //确定请求路径 
  4.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; 
  5.   
  6.     //创建请求对象 
  7.     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
  8.   
  9.     //设置代理 
  10.     //***种设置代理: 
  11.     [NSURLConnection connectionWithRequest:request delegate:self]; 
  12.     //第二种设置代理: 
  13.     NSURLConnection *connecttion1 = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
  14.     //第三种设置代理: 
  15.     NSURLConnection *connecttion2 = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 
  16.   
  17.     [connecttion2 start];   

实现代理中的方法

 

  1. #pragma mark - 代理方法 
  2. //请求失败的时候调用 
  3. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
  4.     NSLog(@"%s------%@",__func__,[NSThread currentThread]); 
  5. //接收响应头信息 
  6. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
  7.     NSLog(@"%s------%@",__func__,[NSThread currentThread]); 
  8.   
  9.     //创建接收可变的二进制数据 
  10.     self.responseData = [NSMutableData data]; 
  11.   
  12. //接收响应体(如果数据足够大那么这个方法会调用多次) 
  13. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
  14.     NSLog(@"%s------%@",__func__,[NSThread currentThread]); 
  15.     //拼接二进制数据 
  16.     [self.responseData appendData:data]; 
  17.   
  18. //接收完成(不管成功还是失败) 
  19. - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
  20.     NSLog(@"%s------%@",__func__,[NSThread currentThread]); 
  21.   
  22.     //解析数据 
  23.     NSLog(@"%@",[[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]); 

五 POST方式

1 直接看代码,上面都标明了

 

  1. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event 
  2.     //创建请求路径 
  3.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"]; 
  4.     //创建可变的请求对象 
  5.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
  6.   
  7.     //修改请求方式 
  8.     request.HTTPMethod = @"POST"
  9.   
  10.     //设置请求超时 
  11.     request.timeoutInterval = 10; 
  12.   
  13.     NSURLResponse *response = nil; 
  14.   
  15.     NSError *error = nil; 
  16.   
  17.     //设置请求头信息 
  18.     [request setValue:@"jjjj" forHTTPHeaderField:@"uuuuu"]; 
  19.   
  20.     //设置请求体(参数) 
  21.     request.HTTPBody = [@"username=520it&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding]; 
  22.   
  23.     //***种方法:发送请求(异步请求) 
  24.     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 
  25.         //解析数据 
  26.         if (connectionError == nil) { 
  27.             NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  28.   
  29.         }else
  30.             NSLog(@"%@",connectionError); 
  31.         } 
  32.     }]; 
  33.   
  34.     //第二种方法:发送请求(同步请求) 
  35.     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
  36.     //解析数据 
  37.     NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 

六 中文转码

  1. 判断需不需要转码操作:看请求路径是否含有中文,含有的话,就需要转码
  2. 设置代理的多一个参数的方法中:如果设置的为NO,那么手动开启的时候,底层start会把线程加入到runloop中,但是如果设置的为yes,那么和没有参数的时候一样,需要手动创建runloop.

GET转码:

 

  1. #pragma mark - GET转码 
  2. - (void)get 
  3.     //确定请求字符串 
  4.     NSString *strurl = @"http://120.25.226.186:32812/login2?username=(需要转的汉字)&pwd=520it&type=JSON"
  5.   
  6.     //转码 
  7.     strurl = [strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
  8.   
  9.     //确定路径 
  10.     NSURL *url = [NSURL URLWithString:strurl]; 
  11.   
  12.     //创建可变的请求对象 
  13.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
  14.   
  15.     //发送请求--->GET请求 
  16.     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 
  17.        //解析数据 
  18.         if (connectionError == nil) { 
  19.             NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  20.         }else
  21.             NSLog(@"%@",connectionError); 
  22.         } 
  23.     }]; 

POST转码:

 

  1. #pragma mark - POST转码 
  2. - (void)post 
  3.     //确定请求路径的字符串 
  4.     NSString *urlstr = @"http://120.25.226.186:32812/login2"
  5.   
  6.     //确定url 
  7.     NSURL *url = [NSURL URLWithString:urlstr]; 
  8.   
  9.     //创建请求对象 
  10.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
  11.   
  12.     //转为POST格式 
  13.     request.HTTPMethod = @"POST"
  14.     //转码 
  15.     request.HTTPBody = [@"username=(需要转的汉字)&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding ]; 
  16.     NSURLResponse *response = nil; 
  17.     NSError *error = nil; 
  18.   
  19.     //发送请求(同步请求) 
  20.     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
  21.     //解析数据 
  22.     NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  23.   
  24.     //发送请求(异步请求) 
  25.     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 
  26.         //解析数据 
  27.         if (connectionError == nil) { 
  28.             NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  29.         }else
  30.             NSLog(@"%@",connectionError); 
  31.         } 
  32.   
  33.     }];     

七 NSURLSession简单使用

1 NSURLSession —->GET用法:

 

  1. - (void)get 
  2.     //确定请求路径 
  3.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; 
  4.     //创建请求对象 
  5.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url ]; 
  6.     //获取会话对象(是一个单例) 
  7.     NSURLSession *session = [NSURLSession sharedSession]; 
  8.   
  9.     //根据会话对象创建task 
  10.     /** 
  11.      *  参数一:请求对象 
  12.      * 
  13.      *  参数二:响应头response信息;响应体data信息;error错误信息 
  14.      * 
  15.      * 
  16.      */ 
  17.     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
  18.         //解析数据 
  19.         NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  20.         //该block块是在子线程中调用 
  21.          NSLog(@"%@",[NSThread currentThread]); 
  22.     } ]; 
  23.   
  24.     //执行task 
  25.     [dataTask resume]; 

2 GET用法二:

 

  1. - (void)get1 
  2.     //确定请求路径 
  3.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; 
  4.   
  5.     //创建请求对象 
  6. //    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
  7.   
  8.     //创建会话对象 
  9.     NSURLSession *session = [NSURLSession sharedSession]; 
  10.   
  11.     //根据会话对象创建task 
  12.     NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
  13.         NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  14.         //该block块是在子线程中调用 
  15.          NSLog(@"%@",[NSThread currentThread]); 
  16.     }]; 
  17.     //开启task 
  18.     [dataTask resume]; 

3 GET1和GET2的区别是根据会话对象创建task不同,其实用法还是一样的.

4 POST用法:

 

  1. - (void)post 
  2.     //确定请求路径 
  3.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"]; 
  4.   
  5.     //创建可变的请求对象 
  6.     NSMutableURLRequest *resquest = [NSMutableURLRequest requestWithURL:url]; 
  7.   
  8.     //转换格式 
  9.     resquest.HTTPMethod = @"POST"
  10.   
  11.     //设置请求体信息 
  12.     resquest.HTTPBody = [@"username=520it&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding]; 
  13.   
  14.     //获取会话 
  15.     NSURLSession *session = [NSURLSession sharedSession]; 
  16.   
  17.     //用会话对象创建task 
  18.     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:resquest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
  19.   
  20.         //解析数据 
  21.         if (error == nil) { 
  22.             NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 
  23.         }else
  24.             NSLog(@"%@",error); 
  25.         } 
  26.         //该block块是在子线程中调用 
  27.         NSLog(@"%@",[NSThread currentThread]); 
  28.     }]; 
  29.   
  30.     //执行task任务   resume------>恢复 
  31.     [dataTask resume]; 

八 NSURLSession的代理方法

1 主方法(在里面设置代理)

 

  1. - (void)sessionGet 
  2.     //确定请求路径 
  3.     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; 
  4.     //创建可变的请求对象 
  5.     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
  6.   
  7.     //获取会话(delegateQueue:决定了代理的任务是在子线程还是主线程中执行的) 
  8.     NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
  9.   
  10.     //创建task 
  11.     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request]; 
  12.     //执行task任务 
  13.     [dataTask resume]; 
  14.   

2 代理方法

注意: 此代理方法要特别的注意***个代理方法,因为需要回调告诉系统怎么样处理服务器返回的数据,如果没有写的话,后面两个代理方法是不会调用的,因为completionHandler并没有告诉系统,服务器的数据怎么处理.

 

  1. #pragma mark - 代理方法 
  2. //接收响应 
  3. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler 
  4.     NSLog(@"%s-------%@",__func__,[NSThread currentThread]); 
  5.   
  6.     self.data = [NSMutableData data]; 
  7.     //在该方法中需要通过completionHandler回调告诉系统应该如何处理服务器返回的数据 
  8.     completionHandler(NSURLSessionResponseAllow); 
  9. //接收到二进制数据(如果数据量大会调用多次) 
  10. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data 
  11.   
  12.     NSLog(@"%s-----%@",__func__,[NSThread currentThread]); 
  13.     //拼接数据 
  14.     [self.data appendData:data]; 
  15.   
  16. //完成或者失败的时候调用 
  17. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error 
  18.     NSLog(@"%s------%@",__func__,[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]); 
  19.     NSLog(@"%@",[NSThread currentThread]); 

 

责任编辑:未丽燕 来源: 简书
相关推荐

2010-03-05 17:28:08

2016-04-06 11:14:48

iOS相机自定义

2011-05-26 09:03:17

JSONjavascript

2009-11-26 11:19:52

NIS服务器

2020-12-18 07:33:20

SpringSchedule组件

2012-06-28 10:26:51

Silverlight

2020-09-14 11:30:26

HTTP3运维互联网

2013-06-24 15:32:41

JPush极光推送Android Pus移动开发

2021-04-30 16:23:58

WebRTC实时音频

2021-12-10 06:52:24

业务

2010-11-03 11:01:05

求职面试

2021-12-01 06:50:50

Docker底层原理

2020-12-09 06:19:39

ReLU神经网络深度学习

2020-10-27 10:43:24

Redis字符串数据库

2014-04-15 11:19:19

2011-07-11 09:58:52

2021-12-02 06:58:03

AIOps通信服务

2020-07-25 19:40:33

Java开发代码

2015-04-21 17:35:29

代码

2023-11-30 10:21:48

虚拟列表虚拟列表工具库
点赞
收藏

51CTO技术栈公众号