NSURLSession 请求
参考网站:http://ningandjiao.iteye.com/blog/2010753
http://www.cocoachina.com/industry/20131106/7304.html
代码部分:
- (void)viewDidLoad {
[super viewDidLoad];
//-----------不能监测下载进度的方法------------
// //创建NSURLSeesion
// NSURLSession *session = [NSURLSession sharedSession];//是一个单例
// //在session中每一个请求都是一个任务
//// NSURLSessionDataTask //做get和post请求的
//// NSURLSessionDownloadTask//用于下载
//// NSURLSessionUploadTask//用于上传
// NSURL *url = [NSURL URLWithString:@"http://nmo.ouj.yymommy.com/185cd4217a3a4799/1441088092/mp3_190_67/30/ac/30e767b8bf61d844ed628ff084b067ac.mp3?s=t"];
// //创建任务
// NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
// NSLog(@"%@",location.path);//NSURL *location为默认路径 location.path转换成
// //session默认将文件下载到temp目录下 下载完会默认移除,所以想要永久保存要将它移植到caches文件夹下
// NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
// NSString *string = [path stringByAppendingPathComponent:@"text1.mp3"];
// //创建文件管理器 移植文件
// NSFileManager *manager = [NSFileManager defaultManager];
//// [manager moveItemAtPath:location.path toPath:string error:nil];
// //也可以用copy的方法
// [manager copyItemAtPath:location.path toPath:string error:nil];
//
//
// }];
// //开启任务
// [task resume];
//----------------------注销线----------------
//-----------------可以监测下载进度的方法----------
//创建默认配置
NSURLSessionConfiguration *cfn = [NSURLSessionConfiguration defaultSessionConfiguration];
//创建下载任务
NSURLSession *session = [NSURLSession sessionWithConfiguration:cfn delegate:self delegateQueue:[NSOperationQueue mainQueue]];//1参 控制器 2参 download代理
//开启下载任务 (如果调用的downloadTaskWithURL带block 那么代理方法不会走)
NSURL *url = [NSURL URLWithString:@"http://nmo.ouj.yymommy.com/185cd4217a3a4799/1441088092/mp3_190_67/30/ac/30e767b8bf61d844ed628ff084b067ac.mp3?s=t"];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];
//开启任务
[task resume];
}
#pragma mark --代理方法
//下载完成会调用的方法
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
}
//每下载一段数据就会调用以下方法
//bytesWritten 这个参数表示这次下载了多少数据
//totalBytesWritten 下载的累计总数据长度
//totalBytesExpectedToWrite 文件总长度
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
}
//断点续传
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes
{
}
NSURLSession 请求的更多相关文章
- 使用NSURLSession请求需要AD认证的HTTPS服务器
关键代码:使用后台下载PDF文件 - (void)startDownloadPDF{ NSURLSession *session = [self session]; NSString *downloa ...
- iOS网络2——NSURLSession使用详解
原文在此 一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台 ...
- iOS网络NSURLSession使用详解
一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台.现在使用 ...
- AFNetworking-2.5-源码阅读剖析--网络请求篇
一.前言 AFNetworking,非常友好简单的网络请求第三方框架,在GitHub中已经获得了25000++的star,链接地址:https://github.com/AFNetworking/AF ...
- Alamofire 的使用
最近,AFNetworking 的作者Mattt Thompson提交了一个新的类似于 AFNetworking 的网络 基础库,并且是专门使用最新的 Swift 语言来编写的,名为:Alamofir ...
- Alamofire网络库基础教程
原文 Beginning Alamofire Tutorial 原文作者 Essan Parto译者 星夜暮晨(QQ:412027805) http://www.jianshu.com/p/f1208 ...
- iOS中的NSURLProtocol
转自:iOS知识小集 NSURLProtocol类(注意,这个不是协议)经常用于实现一些URL Loading System相关的黑魔法.它可以拦截URL Loading System相关的网络请求, ...
- 检查电脑链接的网络是否支持ipv6
测试方法一:在浏览器地址栏输入网址“http://test-ipv6.com/”,在页面会给出您的ipv6网络测试结果 测试方法二:在浏览器地址栏输入网址“http://ipv6.jmu.edu.cn ...
- NSURLSession网络请求
个人感觉在网上很难找到很简单的网络请求.或许是我才疏学浅 , 所有就有了下面这一段 , 虽然都是代码 , 但是全有注释 . //1/获取文件访问路径 NSString *path=@"ht ...
随机推荐
- Python日期时间函数处理
所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...
- 【JMeter】Jmeter引入第三方jar包
Jmeter做remoteService,里面用到一个实体:clickEntity,是在一个第三方jar包定义的:com.bj58.opt.ad_logparser-0.0.18-SNAPSHOT.j ...
- C#操作Json(转)
原文:http://wenku.baidu.com/link?url=3dlqKdF26ZdQIAcX9jvP2ZYEtl3J0sKOV8XuHQI0Rz4SjB9S46nqmGiMXUVQa_1Pm ...
- javascript设计模式5
子类引用父类 function extend(subClass,superClass){ var F=function(){}; F.prototype=superClass.prototype; s ...
- Petshop学习第三天
ASP.NET缓存 ASP.NET充分利用缓存机制,通过某种方法,将系统需要的数据对象.Web页面存储在内存中,使得Web站点需要这些数据时,不经过繁琐的数据库连接.查询和复杂的逻辑运算,就可以触手可 ...
- mapreduce学习指导及疑难解惑汇总
原文链接http://www.aboutyun.com/thread-7091-1-1.html 1.思想起源: 我们在学习mapreduce,首先我们从思想上来认识.其实任何的奇思妙想,抽象的,好的 ...
- [OC Foundation框架 - 1] 常用结构体
底层封装是使用了typedef定义的结构体 typedef struct _NSString{ xxx xxx } NSString; 1. NSRange 结构体 #注意结构体不是对象 3种定义 ...
- gulp 基础运用
全局安装gulp $npm install --global gulp 作为项目的开发依赖安装 //--save-dev 开发依赖,储存在package.json的devDependencies中,如 ...
- sql语句常见错误
1.两张表关联用的三种连接: left join .right join .inner join区别 (备注:两个表链接肯定是根据两个表(如A B)中的两个字段值(如A.bId和B.id),相等就行 ...
- 压缩UI深度的代码实现
记录一下,或许同样使用深度的NGUI以后会用到. 目前的项目的UI是用Stage3D实现的,采用了类似NGUI填写深度来确定覆盖关系,但同时可以使用的深度是有一个固定范围的,导致的问题是如果UI过多深 ...