zoukankan      html  css  js  c++  java
  • ios NSURLSession

    ios NSURLSession使用说明及后台工作流程分析

     分类:
     

    转自:http://www.maxiaoguo.com/clothes/268.html

    NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的。在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作。注意,如果用户强制将程序关闭,NSURLSession会断掉。

     
    NSURLSession提供的功能:
    1.通过URL将数据下载到内存
    2.通过URL将数据下载到文件系统
    3.将数据上传到指定URL
    4.在后台完成上述功能
     
    工作流程
    如果我们需要利用NSURLSession进行数据传输我们需要:
    1.创建一个NSURLSessionConfiguration,用于第二步创建NSSession时设置工作模式和网络设置:
    工作模式分为:
    一般模式(default):工作模式类似于原来的NSURLConnection,可以使用缓存的Cache,Cookie,鉴权。
    及时模式(ephemeral):不使用缓存的Cache,Cookie,鉴权。
    后台模式(background):在后台完成上传下载,创建Configuration对象的时候需要给一个NSString的ID用于追踪完成工作的Session是哪一个(后面会讲到)。
     
    网络设置:参考NSURLConnection中的设置项。
    1. 创建一个NSURLSession,系统提供了两个创建方法:
    sessionWithConfiguration:
    sessionWithConfiguration:delegate:delegateQueue:
        
    第一个粒度较低就是根据刚才创建的Configuration创建一个Session,系统默认创建一个新的OperationQueue处理Session的消息。
     
    第二个粒度比较高,可以设定回调的delegate(注意这个回调delegate会被强引用),并且可以设定delegate在哪个OperationQueue回调,如果我们将其设置为[NSOperationQueue mainQueue]就能在主线程进行回调非常的方便。
     
    2.创建一个NSURLRequest调用刚才的NSURLSession对象提供的Task函数,创建一个NSURLSessionTask。
     
    根据职能不同Task有三种子类:
    NSURLSessionUploadTask:上传用的Task,传完以后不会再下载返回结果;
    NSURLSessionDownloadTask:下载用的Task;
    NSURLSessionDataTask:可以上传内容,上传完成后再进行下载。
     
    得到的Task,调用resume开始工作。
     
    3.如果是细粒度的Session调用,Session与Delegate会在指定的OperationQueue中进行交互,以咱们下载例子,交互过程的顺序图如下(假如不需要鉴权,即非HTTPS请求):
    4.当不再需要连接调用Session的invalidateAndCancel直接关闭,或者调用finishTasksAndInvalidate等待当前Task结束后关闭。这时Delegate会收到URLSession:didBecomeInvalidWithError:这个事件。Delegate收到这个事件之后会被解引用。
     
    5.如果是一个BackgroundSession,在Task执行的时候,用户切到后台,Session会和ApplicationDelegate做交互。当程序切到后台后,在BackgroundSession中的Task还会继续下载,这部分文档叙述比较少,现在分三个场景分析下Session和Application的关系:
     
    1)当加入了多个Task,程序没有切换到后台。
    这种情况Task会按照NSURLSessionConfiguration的设置正常下载,不会和ApplicationDelegate有交互。
     
    2)当加入了多个Task,程序切到后台,所有Task都完成下载。
     
    在切到后台之后,Session的Delegate不会再收到,Task相关的消息,直到所有Task全都完成后,系统会调用ApplicationDelegate的application:handleEventsForBackgroundURLSession:completionHandler:回调,之后“汇报”下载工作,对于每一个后台下载的Task调用Session的Delegate中的URLSession:downloadTask:didFinishDownloadingToURL:(成功的话)和URLSession:task:didCompleteWithError:(成功或者失败都会调用)。
     
    之后调用Session的Delegate回调URLSessionDidFinishEventsForBackgroundURLSession:。
     
    注意:在ApplicationDelegate被唤醒后,会有个参数ComplietionHandler,这个参数是个Block,这个参数要在后面Session的Delegate中didFinish的时候调用一下,如下:
    1. @implementation APLAppDelegate 
    2.  
    3.   
    4.  
    5. - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier 
    6.  
    7.   completionHandler:(void (^)())completionHandler 
    8.  
    9.  
    10.     BLog(); 
    11.  
    12.     /* 
    13.  
    14.      Store the completion handler. The completion handler is invoked by the view controller's checkForAllDownloadsHavingCompleted method (if all the download tasks have been completed). 
    15.  
    16.      */ 
    17.  
    18.       self.backgroundSessionCompletionHandler = completionHandler; 
    19.  
    20.  
    21. //…… 
    22.  
    23. @end 
    24.  
    25.   
    26.  
    27. //Session的Delegate 
    28.  
    29. @implementation APLViewController 
    30.  
    31.   
    32.  
    33. - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session 
    34.  
    35.  
    36.     APLAppDelegate *appDelegate = (APLAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    37.  
    38.     if (appDelegate.backgroundSessionCompletionHandler) { 
    39.  
    40.         void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler; 
    41.  
    42.         appDelegate.backgroundSessionCompletionHandler = nil; 
    43.  
    44.         completionHandler(); 
    45.  
    46.     } 
    47.  
    48.   
    49.  
    50.     NSLog(@"All tasks are finished"); 
    51.  
    52.  
    53. @end 
     
    3)当加入了多个Task,程序切到后台,下载完成了几个Task,然后用户又切换到前台。(程序没有退出)
      
    切到后台之后,Session的Delegate仍然收不到消息。在下载完成几个Task之后再切换到前台,系统会先汇报已经下载完成的Task的情况,然后继续下载没有下载完成的Task,后面的过程同第一种情况。
     
    4)当加入了多个Task,程序切到后台,几个Task已经完成,但还有Task还没有下载完的时候关掉强制退出程序,然后再进入程序的时候。(程序退出了)
     
    最后这个情况比较有意思,由于程序已经退出了,后面没有下完Session就不在了后面的Task肯定是失败了。但是已经下载成功的那些Task,新启动的程序也没有听“汇报”的机会了。经过实验发现,这个时候之前在NSURLSessionConfiguration设置的NSString类型的ID起作用了,当ID相同的时候,一旦生成Session对象并设置Delegate,马上可以收到上一次关闭程序之前没有汇报工作的Task的结束情况(成功或者失败)。但是当ID不相同,这些情况就收不到了,因此为了不让自己的消息被别的应用程序收到,或者收到别的应用程序的消息,起见ID还是和程序的Bundle名称绑定上比较好,至少保证唯一性。
     
    总结
    就像前面说的,在普通的应用场景下NSURLSession与NSURLConnection相比没有什么优势,但是在程序切换到后台之后Background的Session就显得更加灵活了。
     
    另外,现在主流的网络开发框架AFNetworking已经更新到了2.0(只支持iOS 6 / iOS 7),其中最重要的一个更新就是添加了NSURLSession相关的支持。虽然就我现在(2013.10.13)看到他们的源码中,还没有完全的支持后台的Session(或者说没有考虑全我上述的后台情况),但是大家有兴趣可以关注一下他们后续的更新情况。
     
    [objc] view plaincopy
     
    1. //////////////////////  

    代码演示
     01.URLSession 上传,注意代理是 NSURLSessionTaskDelegate
    [objc] view plaincopy
     
    1. //  
    2. //  MJViewController.m  
    3. //  01.URLSession 上传  
    4. //  
    5. //  Created by apple on 14-4-30.  
    6. //  Copyright (c) 2014年 itcast. All rights reserved.  
    7. //  
    8.   
    9. #import "MJViewController.h"  
    10.   
    11. @interface MJViewController () <NSURLSessionTaskDelegate>  
    12.   
    13. @end  
    14.   
    15. @implementation MJViewController  
    16.   
    17. - (void)viewDidLoad  
    18. {  
    19.     [super viewDidLoad];  
    20.   
    21.     [self uploadFile1];  
    22. }  
    23.   
    24. #pragma mark - 监控上传进度  
    25. - (void)uploadFile1  
    26. {  
    27.     // 1. URL  
    28.     NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"head8.png" withExtension:nil];  
    29.     NSURL *url = [NSURL URLWithString:@"http://localhost/uploads/1.png"];  
    30.       
    31.     // 2. Request  
    32.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];  
    33.     // 1> PUT方法  
    34.     // PUT  
    35.     //    1) 文件大小无限制  
    36.     //    2) 可以覆盖文件  
    37.     // POST  
    38.     //    1) 通常有限制2M  
    39.     //    2) 新建文件,不能重名  
    40.     request.HTTPMethod = @"PUT";  
    41.       
    42.     // 2> 安全认证  
    43.     // admin:123456  
    44.     // result base64编码  
    45.     // Basic result  
    46.     /** 
    47.      BASE 64是网络传输中最常用的编码格式 - 用来将二进制的数据编码成字符串的编码方式 
    48.       
    49.      BASE 64的用法: 
    50.      1> 能够编码,能够解码 
    51.      2> 被很多的加密算法作为基础算法 
    52.      */  
    53.     NSString *authStr = @"admin:123456";  
    54.     NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];  
    55.     NSString *base64Str = [authData base64EncodedStringWithOptions:0];  
    56.     NSString *resultStr = [NSString stringWithFormat:@"Basic %@", base64Str];  
    57.     [request setValue:resultStr forHTTPHeaderField:@"Authorization"];  
    58.       
    59.     // 3. Session,全局单例(我们能够给全局的session设置代理吗?如果不能为什么?)  
    60.     // sharedSession是全局共享的,因此如果要设置代理,需要单独实例化一个Session  
    61.     /** 
    62.      NSURLSessionConfiguration(会话配置) 
    63.       
    64.      defaultSessionConfiguration;       // 磁盘缓存,适用于大的文件上传下载 
    65.      ephemeralSessionConfiguration;     // 内存缓存,以用于小的文件交互,GET一个头像 
    66.      backgroundSessionConfiguration:(NSString *)identifier; // 后台上传和下载 
    67.      */  
    68.     NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];  
    69.       
    70.     NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc]init]];  
    71.       
    72.     // 需要监听任务的执行状态  
    73.     NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:fileURL];  
    74.       
    75.     // 4. resume  
    76.     [task resume];  
    77. }  
    78.   
    79. #pragma mark - 上传进度的代理方法  
    80. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend  
    81. {  
    82.     // bytesSent totalBytesSent totalBytesExpectedToSend  
    83.     // 发送字节(本次发送的字节数)    总发送字节数(已经上传的字节数)     总希望要发送的字节(文件大小)  
    84.     NSLog(@"%lld-%lld-%lld-", bytesSent, totalBytesSent, totalBytesExpectedToSend);  
    85.     // 已经上传的百分比  
    86.     float progress = (float)totalBytesSent / totalBytesExpectedToSend;  
    87.     NSLog(@"%f", progress);  
    88. }  
    89.   
    90. #pragma mark - 上传完成的代理方法  
    91. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error  
    92. {  
    93.     NSLog(@"完成 %@", [NSThread currentThread]);  
    94. }  
    95. @end  
    02.Session下载
    [objc] view plaincopy
     
    1. //  
    2. //  MJViewController.m  
    3. //  02.Session下载  
    4. //  
    5. //  Created by apple on 14-4-30.  
    6. //  Copyright (c) 2014年 itcast. All rights reserved.  
    7. //  
    8.   
    9. #import "MJViewController.h"  
    10.   
    11. @interface MJViewController () <NSURLSessionDownloadDelegate>  
    12. @property (weak, nonatomic) IBOutlet UIImageView *imageView;  
    13. @end  
    14.   
    15. /** 
    16.  // 下载进度跟进 
    17.  - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask 
    18.  didWriteData:(int64_t)bytesWritten 
    19.  totalBytesWritten:(int64_t)totalBytesWritten 
    20.  totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite; 
    21.   
    22.  didWriteData totalBytesWritten totalBytesExpectedToWrite 
    23.  本次写入的字节数 已经写入的字节数   预期下载的文件大小 
    24.   
    25.  // 完成下载 
    26.  - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask 
    27.  didFinishDownloadingToURL:(NSURL *)location; 
    28.  */  
    29.   
    30. @implementation MJViewController  
    31.   
    32. - (void)viewDidLoad  
    33. {  
    34.     [super viewDidLoad];  
    35.       
    36.     [self downloadTask];  
    37. }  
    38.   
    39. #pragma mark - 下载(GET)  
    40. - (void)downloadTask  
    41. {  
    42.     // 1. URL  
    43.     NSURL *url = [NSURL URLWithString:@"http://localhost/itcast/images/head1.png"];  
    44.       
    45.     // 2. Request  
    46.     NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0];  
    47.       
    48.     // 3. Session  
    49.     NSURLSession *session = [NSURLSession sharedSession];  
    50.       
    51.     // 4. download  
    52.     [[session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {  
    53.         // 下载的位置,沙盒中tmp目录中的临时文件,会被及时删除  
    54.         NSLog(@"下载完成 %@ %@", location, [NSThread currentThread]);  
    55.         /** 
    56.          document       备份,下载的文件不能放在此文件夹中 
    57.          cache          缓存的,不备份,重新启动不会被清空,如果缓存内容过多,可以考虑新建一条线程检查缓存目录中的文件大小,自动清理缓存,给用户节省控件 
    58.          tmp            临时,不备份,不缓存,重新启动iPhone,会自动清空 
    59.          */  
    60.         // 直接通过文件名就可以加载图像,图像会常驻内存,具体的销毁有系统负责  
    61.         // [UIImage imageNamed:@""];  
    62.         dispatch_async(dispatch_get_main_queue(), ^{  
    63.             // 从网络下载下来的是二进制数据  
    64.             NSData *data = [NSData dataWithContentsOfURL:location];  
    65.             // 这种方式的图像会自动释放,不占据内存,也不需要放在临时文件夹中缓存  
    66.             // 如果用户需要,可以提供一个功能,保存到用户的相册即可  
    67.             UIImage *image = [UIImage imageWithData:data];  
    68.               
    69.             self.imageView.image = image;  
    70.         });  
    71.     }] resume];  
    72.       
    73. //    [task resume];  
    74. }  
    75.   
    76. @end  
  • 相关阅读:
    MySql存储过程学习
    自己用C语言写的扫雷算法
    Spring学习——Hello World
    ICE Service使用方法简介
    DevExpress学习笔记(一)Ribbon
    DevExpress学习笔记(二)NavBarControl
    ORACLE DBLINK无法使用问题
    vbs脚本读写INI文件
    Python操作INI文件:configobj 更好
    个人发展的误区:越广越好,还是越深越好?
  • 原文地址:https://www.cnblogs.com/developer-ios/p/4985568.html
Copyright © 2011-2022 走看看