NSURLSession是从IOS7.0出来代替NSURLConnection。Session发布三种任务。DataTask,UploadTask,DownloadTask,而且任务都是默认挂起的。须要Resume。所谓挂起,就是任务执行(下载)一半时候能够总体中止,挂起。当须要的时候再继续。而之前的Connnection中的断点续传工做直接就是把connection取消掉了。json
(1)当使用get请求是,NSURLRequest能够省略。网络
(2)session提供了单例的方法。session
(3)NSURLConnection常规网络请求步骤:app
1.拿到URL框架
2.NSURLRequest性能
3.NSURLConnectionurl
(4)NSURLSession常规网络请求步骤:spa
1.拿到URL代理
2.NSURLSession单例code
3.session任务发布
4.启动任务resume
(5)DataTask代码实现:
1 - (void)session{ 2 3 //1.url 4 NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.json"]; 5 6 //2.3.4单例全局session发布任务而且执行 7 [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 8 // 回调 9 10 NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]); 11 12 }] resume]; 13 14 }
(6)DownLoadTask
从IOS7.0开始,程序使用FreamWork不须要添加引用。只须要import就行。Xcode会自动添加。可是动态库须要手动添加。
1.动态库:须要手动添加 。内存中只有一个,性能好,安装在系统级的位置中,供全部app使用。
2.静态库:不一样的app,内存副本有不少。哪一个app须要就copy处一个。(支付宝什么的是静态库---由于动态库没法上架)。
因为当前的不少下载都是zip格式,因此Session默认downLoad方法是放到tmp,而后删除的。由于这是苹果作的一个默认的压缩包删除。因此此时要在Session任务的回调时,对数据进行解压缩。这时候用到了一个第三方框架---SSZipArchive
SSZipArchive框架解析:
1 // Unzip 2 3 // 将文件解压缩到指定目录 4 + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination; 5 // 将文件解压缩到指定目录,同时指定密码 6 + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error; 7 // 将文件解压缩到指定目录,而且设置代理(回调解压缩进度) 8 + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id<SSZipArchiveDelegate>)delegate; 9 // 将文件解压缩到指定目录,同时指定密码,而且设置代理 10 + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id<SSZipArchiveDelegate>)delegate; 11 12 // Zip 13 + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames; 14 + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath; 15 16 - (id)initWithPath:(NSString *)path; 17 - (BOOL)open; 18 - (BOOL)writeFile:(NSString *)path; 19 - (BOOL)writeData:(NSData *)data filename:(NSString *)filename; 20 - (BOOL)close; 21 22 @end 23 24 25 @protocol SSZipArchiveDelegate <NSObject> 26 27 @optional 28 29 // 代理 30 - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo; 31 - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath; 32 33 - (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo; 34 - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo; 35 36 - (void)zipArchiveProgressEvent:(NSInteger)loaded total:(NSInteger)total; 37 @end
(7)进度跟进
当跟进进入时候,就没法使用全局Session,所有Session是为全部应用程序服务的。而跟踪进度是须要用到代理,代理是1对1的。因此通常都会当成属性来建立一个Session。