码迷,mamicode.com
首页 > Web开发 > 详细

NSURLSession

时间:2015-07-24 09:10:39      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

  NSURLSession是从IOS7.0出来代替NSURLConnection。Session发布三种任务。DataTask,UploadTask,DownloadTask,并且任务都是默认挂起的。需要Resume。所谓挂起,就是任务执行(下载)一半时候可以整体停止,挂起。当需要的时候再继续。而以前的Connnection中的断点续传工作直接就是把connection取消掉了。

(1)当使用get请求是,NSURLRequest可以省略。

(2)session提供了单例的方法。

(3)NSURLConnection常规网络请求步骤:

  1.拿到URL

  2.NSURLRequest

  3.NSURLConnection

(4)NSURLSession常规网络请求步骤:

  1.拿到URL

  2.NSURLSession单例

  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。

  

 

NSURLSession

标签:

原文地址:http://www.cnblogs.com/aixiaoxin/p/4672358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!