2016 - 1 - 24 NSURLSession (一)
一: 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 (一)的更多相关文章
- 2016.3.24 OneZero站立会议
会议时间:2016.3.24 15:35-15:55 会议成员:王巍 夏一名 冉华 张敏 会议内容: 1.确立UI界面原形(见http://www.cnblogs.com/zhangminss/p/5 ...
- Murano Weekly Meeting 2016.05.24
Meeting time: 2016.May.24 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1.A ...
- 2016.9.24初中部上午NOIP普及组比赛总结
2016.9.24初中部上午NOIP普及组比赛总结 2016.09.24[初中部 NOIP普及组 ]模拟赛 其实这次我没比赛,早上去参加亲子活动去了. 不过在下午我做完了所有的题,感觉还好. 进度 现 ...
- OneZero第四次站立会议(2016.3.24)
会议时间:2016年3月24日 15:30~15:47 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会后修改内容. 会议内容:以下为会议插图 1.界面原型方面,在 ...
- “耐撕”团队 2016.03.24 站立会议
时间: 2016.03.22 17:00-17:30 18:30-19:00 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), ...
- 2016 - 1- 21 - RunLoop使用(2016-1-24修改一次)&(2016 - 1 - 24 再次修改)
一:常驻线程 :当需要一个线程一直处理一些耗时操作时,可以让它拥有一个RunLoop.具体代码如下: 1.通过给RunloopMode里加源来保证RunLoop不直接退出. 这里有个很重要得知识 ...
- 2016/2/24 1,dotctype有几种? 2,了解html的发展历史
1,dotctype有几种?DOCTYPE是document type(文档类型)的简写,用来说明你用的XHTML或者HTML是什么版本. 其中的DTD(例如上例中的xhtml1-transition ...
- 来自于2016.2.24的flag
今天又做了一套xj模拟题-------打比赛这种事情变得越来越无聊了------既影响自己的计划(虽然看起来很难完成的样子),又扰乱心情.而且题目大都是学习算法之类的,与计划不接轨就非常没有兴趣. 然 ...
- 2016 - 1 - 24 CSS初步
1.The difference between CSS and HTML HTML document is that it specities the content of the page. An ...
随机推荐
- CentOS7安装Apache2.4+PHP5.6
linux系统CentOS7 先下载Apache需要依赖的软件 1.APR 下载地址http://apr.apache.org/download.cgi wget下载路径http://mirror.b ...
- Just a Hook(HDU1698 线段树的简单应用)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- Linux phpwind论坛的安装
1:新建文件夹phpwind
- [Linux] VIM 常用快捷键2
如何使用MacVim 1.在插入模式之外 基本上来说,你应该尽可能少的呆在插入模式里面,因为在插入模式里面 VIM 就像一个“哑巴”编辑器一样.很多新手都会一直呆在插入模式里面,因为这样易于使用.但 ...
- iphone 3gs 美版,6.1.3+降基带+越狱+解锁。成功分享(转)
本人参照这个帖子成功把一个白苹果的机器救活了 2014年1月26日 13点 转自:http://bbs.app111.com/thread-510632-1-1.html 时间:2013年5月31日 ...
- SQL MD5加密
) 加密结果:
- git 临时记录
http://blog.csdn.net/wangbole/article/details/8552808 http://blog.csdn.net/gq414047080/article/detai ...
- 程序设计入门——C语言 第2周编程练习 1时间换算(5分)
1 时间换算(5分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果 ...
- 手势抽取过程&代码复用
public abstract class BaseSetupActivity extends Activity { private GestureDetector gestureDetector; ...
- 使用Android点击按钮跳转页面
1.首先新建一个Android工程,命名为MyApp(名字可以自己随意起); 2.以原有的MainActivity.java文件为登录界面,然后在src文件中的包上面右击选择New目录下的Other中 ...