参考网站: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 请求的更多相关文章

  1. 使用NSURLSession请求需要AD认证的HTTPS服务器

    关键代码:使用后台下载PDF文件 - (void)startDownloadPDF{ NSURLSession *session = [self session]; NSString *downloa ...

  2. iOS网络2——NSURLSession使用详解

    原文在此 一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台 ...

  3. iOS网络NSURLSession使用详解

    一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台.现在使用 ...

  4. AFNetworking-2.5-源码阅读剖析--网络请求篇

    一.前言 AFNetworking,非常友好简单的网络请求第三方框架,在GitHub中已经获得了25000++的star,链接地址:https://github.com/AFNetworking/AF ...

  5. Alamofire 的使用

    最近,AFNetworking 的作者Mattt Thompson提交了一个新的类似于 AFNetworking 的网络 基础库,并且是专门使用最新的 Swift 语言来编写的,名为:Alamofir ...

  6. Alamofire网络库基础教程

    原文 Beginning Alamofire Tutorial 原文作者 Essan Parto译者 星夜暮晨(QQ:412027805) http://www.jianshu.com/p/f1208 ...

  7. iOS中的NSURLProtocol

    转自:iOS知识小集 NSURLProtocol类(注意,这个不是协议)经常用于实现一些URL Loading System相关的黑魔法.它可以拦截URL Loading System相关的网络请求, ...

  8. 检查电脑链接的网络是否支持ipv6

    测试方法一:在浏览器地址栏输入网址“http://test-ipv6.com/”,在页面会给出您的ipv6网络测试结果 测试方法二:在浏览器地址栏输入网址“http://ipv6.jmu.edu.cn ...

  9. NSURLSession网络请求

    个人感觉在网上很难找到很简单的网络请求.或许是我才疏学浅 ,  所有就有了下面这一段 , 虽然都是代码 , 但是全有注释 . //1/获取文件访问路径 NSString *path=@"ht ...

随机推荐

  1. less命令

    less命令的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允许用户向前或向后浏览文件,而more命令只能向前浏览.用less命令显示文件时,用PageUp键向上翻页,用P ...

  2. mssql server 2005还原数据库bak文件与“备份集中的数据库备份与现有的xx数据库不同”解决方法

    mssql server 2005还原数据库bak文件,网站使用虚拟主机建站会经常遇到,一般情况下,主机商有在线的管理程序,但有时候没有的话,就需要本地还原备份sql数据库了.这种情况mssql se ...

  3. new和delete为什么要匹配

    operator new和operator delete函数有两个重载版本: void* operator new (size_t); // allocate an object void* oper ...

  4. Android 网络权限配置

    Android开发应用程序时,如果应用程序需要访问网络权限,需要在 AndroidManifest.xml 中加入以下代码 <uses-permission android:name=”andr ...

  5. CentOS 6.5 安装配置

    关于CentOS的安装,网上有很多详细的教程.其实重点就在于硬盘的分区和软件的定制这两块.下面我在VirtualBox虚拟机上安装 CentOS-6.5-i386-minimal. 1.在起始菜单处选 ...

  6. Bzoj-2818 Gcd 欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...

  7. 射频识别技术漫谈(4)——数据编码【worldsing 笔记】

    前已述及,射频识别技术中的调制方法一般使用调幅(AM),也就是将有用信号调制在载波的幅度上传送出去.这里的"有用信号"指用高低电平表示的数据"0"或" ...

  8. 锋利的jquery第二版学习笔记

    jquery系统学习笔记 一.初识:jquery的优势:1.轻量级(压缩后不到30KB)2.强大的选择器(支持css1.css2选择器的全部 css3的大部分 以及一些独创的 加入插件的话还可支持XP ...

  9. RocketMQ常用命令

    转自:http://jameswxx.iteye.com/blog/2091971 1.1. 控制台使用 RocketMQ 提供有控制台及一系列控制台命令,用于管理员对主题,集群,broker 等信息 ...

  10. delete table 和 truncate table

    delete table 和 truncate table 使用delete语句删除数据的一般语法格式: delete [from] {table_name.view_name} [where< ...