NSURLResponse下载
2024-10-15 12:23:20
//
// ViewController.m
// 05-NSURLConnestion(下载)
//
// Created by jerry on 15/10/24.
// Copyright (c) 2015年 jerry. All rights reserved.
// #import "ViewController.h"
/**
*
NSURLConnection存在的问题,ios2.0就有了,专门用来负责网络数据的传输,已经有10年的历史
特点:
- 处理简单的网络操作,非常简单,
- 但是处理复杂的网络操作就非常麻烦,
ASI&AFN
iOS 5.0之前,网络下载是一个黑暗的时代:
iOS 5.0之前通过代理的方式来进行处理网络数据的。
存在的问题:
1.下载的过程中,没有进度的跟进,导致用户体验很不好。
2.存在内存峰值 解决办法:
1.通过代理的方式来处理网络数据。 代理方法还是出现峰值:
是因为系统在全部接受完毕之后才去写入,想要解决这个问题,我们可以边接收,边写入
*/
/**
* NSURLConnectionDownloadDelegate 只适合杂志的下载
*/
@interface ViewController ()<NSURLConnectionDataDelegate>
// 下载文件的总长度
@property(nonatomic,assign)long long expectedContentLength; // 当前下载长度
@property(nonatomic,assign)long long currentLength;
// 接收到的数据,用于数据拼接,等所有数据拼接完,写入磁盘
@property(nonatomic,strong)NSMutableData *receiveData;
@end @implementation ViewController - (NSMutableData *)receiveData
{
if (_receiveData == nil) {
_receiveData = [NSMutableData data];
}
return _receiveData;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 下载用get
// 1,url NSString *urlStr = @"http://127.0.0.1/demo.json";
// 百分号的转译
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlStr];
// 2.请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy: timeoutInterval:2.0f];
/**
* 通过代理解决,就不能使用block
*/
// // 连接
// [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//
// //把下载的数据放在桌面
// [data writeToFile:@"/Users/jerry/Desktop/123.m4v" atomically:YES];
// NSLog(@"结束");
// }];
// 3.连接
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self]; // 4.启动网络连接
[connect start];
}
#pragma mark -- 实现代理方法 // 1.接收到服务器的响应,做好准备,开始接收数据
/**
* - (instancetype)initWithURL:(NSURL *)URL MIMEType:(NSString *)MIMEType expectedContentLength:(NSInteger)length textEncodingName:(NSString *)name;
NSURLResponse 响应
URL:请求的资源路径。
MIMEType:(Content-Type)类型返回的二进制数据类型。
expectedContentLength:预期文件的长度。对于下载来说就是文件的大小
textEncodingName:文本的编码名称。(utf-8)
****** utf-8 - 几乎涵盖了全世界两百多个国家的语言文字。
gb2312 - 国内的老的网站还在使用这个编码,包含六千七百多个汉字。
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"文件的大小:%lld",response.expectedContentLength); // 记录文件总程度
self.expectedContentLength = response.expectedContentLength; // 将下载的长度清零
self.currentLength = ; } // 2.接收到服务器返回的数据 拼接数据。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"文件长度:%tu",data.length); // 记录当前已经下载的文件的长度。
self.currentLength += data.length; float progress = (float)self.currentLength/self.expectedContentLength;
NSLog(@"%f",progress);
// 拼接数据
[self.receiveData appendData:data];
} // 3.接受数据完毕,所有数据传输完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"接受数据完毕,所有数据传输完毕"); // 写入磁盘
[self.receiveData writeToFile:@"/Users/jerry/Desktop/123.json" atomically:YES];
// 释放内存
self.receiveData = nil;
}
// 4.下载过程中出现错误
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"下载过程中出现错误");
} @end
最新文章
- ffmpeg-20160908[09,10,13,15,19,21,22,24]-bin.7z
- 详解Js中文件读取机制
- 分享一个漂亮WPF界面框架创作过程及其源码(转)
- java中的null和";";区别
- ON_NOTIFY_REFLECT : Message Reflection for Windows Controls
- Session初识
- Java异常的深入研究与分析
- Qt入门之信号与槽机制
- windows mobile 6.5 隐藏 左下角(左上角)的开始按钮 叉号关闭按钮
- Python数据类型list(列表)和tuple(元组)
- vb socket的使用
- java 读写excle
- CAAnimation动画--(旋转/缩放/移动/闪烁)
- lambda函数常见用法
- [BZOJ 3992] [SDOI 2015] 序列统计
- h5页面
- JDK8下Object类源码理解
- JSON与XML之间的转换
- 关于InnoDB的一些认识
- CMDB三大绝招,助我站稳运维之巅
热门文章
- 自学Linux Shell18.2-sed编辑器高级特性
- 自学Python5.1-面向对象与面向过程
- 3分钟上手log4net
- cf1000D Yet Another Problem On a Subsequence (dp)
- SIEVE 线性筛
- 自动生成Excel 报表工具类
- nginx下后端节点realserverweb健康检测模块ngx_http_upstream_check_module
- 弹指之间 -- Polychord
- 决策树--信息增益,信息增益比,Geni指数的理解
- yolo3的改变