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 ...
随机推荐
- 模拟post请求方法
2
- redis-3.0.0集群的安装及使用
redis集群需要至少6个节点(偶数节点),3个主节点,3个从节点.注意:集群模式最好不要keys *查询数据. 1 下载redis,官网下载3.0.0版本,之前2.几的版本不支持集群模式.下载地址: ...
- Oracle学习网址
Oracle Error Search: http://www.ora-error.com/ Oracle Database Error Message - Oracle Documentation: ...
- 【Ubuntu Java 开发环境搭建 】
配置环境变量 在终端下: sudo gedit /etc/profile 这里当然有些熟悉ubuntu的朋友也可以用 vim, 刚从windows转过来的朋友还是用gedit看着舒服写. ...
- Java 时间转换问题总结
这几天开发中遇到时间转换出错的问题,特总结如下: ========================================================================= ...
- uva 11275 3D Triangles
题意:三维空间中,给出两个三角形的左边,问是否相交. 面积法判断点在三角形内: #include<cstdio> #include<cmath> #include<cst ...
- 【Hadoop代码笔记】Hadoop作业提交之客户端作业提交
1. 概要描述仅仅描述向Hadoop提交作业的第一步,即调用Jobclient的submitJob方法,向Hadoop提交作业. 2. 详细描述Jobclient使用内置的JobS ...
- leetcode@ [127] Word Ladder (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- 【转】#include_next <filename.h>
转载自 http://bbs.csdn.net/topics/390381450 #include_next仅用于特殊的场合. 它被用于头文件中(#include既可用于头文件中, 又可用于.c文件中 ...
- 我被eclipse的tomcat坑的经历
奇怪的乱码问题: 1.jsp页面utf-8 2.java代码utf-8 3.数据库编码utf-8 4.tomcat server.xml配置URIEncoding="UTF-8" ...