参考网站: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. MySQL中间层 Atlas

    Atlas是由 Qihoo 360,  Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bu ...

  2. devexpress 中Grid 的使用:为零不显示

    如果要让为0的列不显示: this.gridColumn_FAmount.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric; ...

  3. 【暑假】[数学]UVa 1262 Password

    UVa 1262  Password 题目: Password   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  4. MFC中Listbox控件的简单使用

    MFC中listbox控件是为了显示一系列的文本,每个文本占一行.   Listbox控件可以设置属性为: LBS_CHILD   :(默认)子窗口 LBS_Visible :(默认)可视 LBS_M ...

  5. 【原创】setjmp longjump一些注意点及使用方法

    setjmp longjump一些注意点及使用方法 jmp_buf结构体的定义 #define _JBLEN  9typedef struct { int _jb[_JBLEN + 1]; } jmp ...

  6. leetcode@ [34] Search for a Range (STL Binary Search)

    https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...

  7. Apache下安装配置mod_pagespeed模块,轻松完成网站提速

    mod_pagespeed是一个开源的Apache module,它由谷歌开发,通过优化你的网页来减少响应延迟和带宽占用.作用参考ngx_pagespeed功能:http://blog.linuxey ...

  8. tomcat登陆WEB显示无权限问题&& tomcat无限循环启动问题

    tomcat登陆WEB显示无权限问题 The user specified as a definer (”@’%') does not exist 原因分析 因为创建视图使用的是xff@%用户(目前已 ...

  9. SQL2008-查询库中是否存在某表

    select   *   from   sysobjects   where   name= 'N201111B'   and   xtype= 'U'

  10. java学习之路---线程(重点)

     1.多线程是一种并发机制的有效手段.进程和线程一样,都是实现并发的一个基本单位.线程是比进程更小的一种实现单位,线程是在进程的基础上的进一步划分.所谓的多线就是在一个进程中产生了多个线程,这些线程可 ...