一: NSURLSession简介

1.实施步骤

1.1 使用 NSURLSession对象 创建TASK ,然后执行TASK

2.TASK的类型:

二: NSURLSession的简单使用:

- (void)download{
NSURL*url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_15.png"]; // NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSession *session = [NSURLSession sharedSession]; // 创建TASK 下载到location中
NSURLSessionDownloadTask *tast = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"%s",__func__);
NSFileManager *fileManger = [NSFileManager defaultManager]; // 设置下载的文件路径和文件名
NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:response.suggestedFilename];
[fileManger moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];
NSLog(@"%@",file);
}];
[tast resume];
}

三: NSURLSession 的代理

需要注意在接受到响应的代理方法中设置允许处理服务器响应

- (void)delegate{
NSURL*url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_15.png"];
// NSURLRequest *request = [NSURLRequest requestWithURL:url]; // Configuration 配置外形
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; NSURLSessionDataTask *task = [session dataTaskWithURL:url]; [task resume]; }
// 接收到到相应调用的代理方法
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{// Dispositon 处置 NSLog(@"%s",__func__);
// 允许处理服务器相应,
completionHandler(NSURLSessionResponseAllow); }
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
NSLog(@"%s",__func__); }
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
NSLog(@"'%@",error);
NSLog(@"%s",__func__); }

2016 - 1 - 24 NSURLSession (一)的更多相关文章

  1. 2016.3.24 OneZero站立会议

    会议时间:2016.3.24 15:35-15:55 会议成员:王巍 夏一名 冉华 张敏 会议内容: 1.确立UI界面原形(见http://www.cnblogs.com/zhangminss/p/5 ...

  2. Murano Weekly Meeting 2016.05.24

    Meeting time: 2016.May.24 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1.A ...

  3. 2016.9.24初中部上午NOIP普及组比赛总结

    2016.9.24初中部上午NOIP普及组比赛总结 2016.09.24[初中部 NOIP普及组 ]模拟赛 其实这次我没比赛,早上去参加亲子活动去了. 不过在下午我做完了所有的题,感觉还好. 进度 现 ...

  4. OneZero第四次站立会议(2016.3.24)

    会议时间:2016年3月24日 15:30~15:47 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会后修改内容. 会议内容:以下为会议插图 1.界面原型方面,在 ...

  5. “耐撕”团队 2016.03.24 站立会议

    时间: 2016.03.22  17:00-17:30     18:30-19:00 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), ...

  6. 2016 - 1- 21 - RunLoop使用(2016-1-24修改一次)&(2016 - 1 - 24 再次修改)

    一:常驻线程 :当需要一个线程一直处理一些耗时操作时,可以让它拥有一个RunLoop.具体代码如下:    1.通过给RunloopMode里加源来保证RunLoop不直接退出. 这里有个很重要得知识 ...

  7. 2016/2/24 1,dotctype有几种? 2,了解html的发展历史

    1,dotctype有几种?DOCTYPE是document type(文档类型)的简写,用来说明你用的XHTML或者HTML是什么版本. 其中的DTD(例如上例中的xhtml1-transition ...

  8. 来自于2016.2.24的flag

    今天又做了一套xj模拟题-------打比赛这种事情变得越来越无聊了------既影响自己的计划(虽然看起来很难完成的样子),又扰乱心情.而且题目大都是学习算法之类的,与计划不接轨就非常没有兴趣. 然 ...

  9. 2016 - 1 - 24 CSS初步

    1.The difference between CSS and HTML HTML document is that it specities the content of the page. An ...

随机推荐

  1. shell脚本 空格

    1.定义变量时, =号的两边不可以留空格. eg: gender=femal----right gender =femal---–wrong gender= femal---–wrong 2.条件测试 ...

  2. 一个注解方式webSocket demo

    前段时间在研究websocket.其中遇到了一些bug.这里跟大家分享这过程. 首先介绍一下websocket WebSocket是HTML5的一种新协议,实现了浏览器和服务器的双全工通信,能更好的节 ...

  3. IOS new Date() 时间转换失败问题以及其他问题

    一: ios下必须标准格式用'/'分割格式 转换没问题:  new Date('2016/5/30 12:15:20') 二:ie9兼容模式以及以下ie版本 console用前需判断 if(windo ...

  4. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

  5. mfc通过消息传递参数进行程序间通信

    这样的程序主要实现windows平台下两个应用程序间的通信. 最简单的单元分为两个部分,一个是引用的程序,一个是被引用的程序. 一.如果引用他人,需要传递参数找到运行的程序,并且传递消息过去 void ...

  6. VIM 常用错误解决

    1.option ‘omnifunc’ is not set 错误: vim7下Omni completion默认情况下是没有开启的,有时候自定义的vimrc文件会实现自动补齐,例如vim-autoc ...

  7. RCP: P2 Update两个烦人bug和解决办法

    问题 Eclipse新的P2 Update机制,使用起来很方便,如果使用P2 plugin自带的UI,开发者完全不用写任何代码 即可实现application的在线更新. 但是P2 Update至少有 ...

  8. GZFramwork数据库层《二》单据表增删改查(自动生成单据号码)

    运行效果: 使用代码生成器(GZCodeGenerate)生成tb_EmpLeave的Model 生成器源代码下载地址: https://github.com/GarsonZhang/GZCodeGe ...

  9. What are the advantages of ReLU over sigmoid function in deep neural network?

    The state of the art of non-linearity is to use ReLU instead of sigmoid function in deep neural netw ...

  10. openstack Icehouse发布

    OpenStack 2014.1 (Icehouse) Release Notes General Upgrade Notes Windows packagers should use pbr 0.8 ...