NSURLResponse下载

//
//  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:1 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 = 0;
    
}

// 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

 

posted @ 2015-10-28 09:27  。低调ヽ继续  阅读(539)  评论(0编辑  收藏  举报