美文网首页iOS开发学习
iOS网络编程(六)

iOS网络编程(六)

作者: BEYOND黄 | 来源:发表于2017-06-01 15:20 被阅读7次

NSURLConnection实现大文件断点下载:主要通过设置请求头告诉服务器要下载视频某一部分,以及句柄。

#import"ViewController.h"

@interfaceViewController()

@property(weak,nonatomic)IBOutletUIProgressView*progressView;

@property(nonatomic,assign)NSIntegertotalSize;

@property(nonatomic,assign)NSIntegercurrentSize;

/**文件句柄*/

@property(nonatomic,strong)NSFileHandle*handle;

/**沙盒路径*/

@property(nonatomic,strong)NSString*fullPath;

/**连接对象*/

@property(nonatomic,strong)NSURLConnection*connect;

@end

@implementationViewController

- (IBAction)startBtnClick:(id)sender {

[selfdownload];

}

- (IBAction)cancelBtnClick:(id)sender {

[self.connectcancel];

}

- (IBAction)goOnBtnClick:(id)sender {

[selfdownload];

}

//内存飙升

-(void)download

{

//1.url

NSURL*url = [NSURLURLWithString:@"http://www.33lc.com/article/UploadPic/2012-10/2012102514201759594.jpg"];

//2.创建请求对象

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url];

//设置请求头信息,告诉服务器值请求一部分数据range

/*

bytes=0-100

bytes=-100 最后100个字节

bytes=100-请求100之后的所有数据

*/

NSString*range = [NSStringstringWithFormat:@"bytes=%zd-",self.currentSize];

[requestsetValue:rangeforHTTPHeaderField:@"Range"];

NSLog(@"+++++++%@",range);

//3.发送请求

NSURLConnection*connect = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];

self.connect= connect;

}

#pragma mark ----------------------

#pragma mark NSURLConnectionDataDelegate

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response

{

NSLog(@"didReceiveResponse");

//1.得到文件的总大小(本次请求的文件数据的总大小!=文件的总大小)

// self.totalSize = response.expectedContentLength + self.currentSize;

if(self.currentSize>0) {

return;

}

self.totalSize= response.expectedContentLength;

//2.写数据到沙盒中

self.fullPath= [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"123.jpg"];

NSLog(@"%@",self.fullPath);

//3.创建一个空的文件

[[NSFileManagerdefaultManager]createFileAtPath:self.fullPathcontents:nilattributes:nil];

//NSDictionary *dict = [[NSFileManager defaultManager]attributesOfItemAtPath:self.fullPath error:nil];

//4.创建文件句柄(指针)

self.handle= [NSFileHandlefileHandleForWritingAtPath:self.fullPath];

}

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data

{

//1.移动文件句柄到数据的末尾

[self.handleseekToEndOfFile];

//2.写数据

[self.handlewriteData:data];

//3.获得进度

self.currentSize+= data.length;

//进度=已经下载/文件的总大小

NSLog(@"%f",1.0*self.currentSize/self.totalSize);

self.progressView.progress=1.0*self.currentSize/self.totalSize;

//NSLog(@"%@",self.fullPath);

}

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error

{

}

-(void)connectionDidFinishLoading:(NSURLConnection*)connection

{

//1.关闭文件句柄

[self.handlecloseFile];

self.handle=nil;

NSLog(@"connectionDidFinishLoading");

NSLog(@"%@",self.fullPath);

}

@end

相关文章

  • iOS网络编程(六)

    NSURLConnection实现大文件断点下载:主要通过设置请求头告诉服务器要下载视频某一部分,以及句柄。 #i...

  • iOS网络编程(四):缓存类NSURLCache介绍

    网络请求中缓存类NSURLCache常用属性和方法: 参考:iOS网络编程之六——数据缓存类NSURLCache使用解析

  • iOS搭建Socket服务器的相关方法

    iOS网络编程层次 iOS网络编程层次结构也分为三层: Cocoa层:NSURL,Bonjour,Game Kit...

  • Socket

    Socket iOS网络编程层次结构 iOS网络编程层次结构分为三层,从上往下依次为: Cocoa层:NSURL,...

  • iOS关于HTTP协议和网络编程

    1.网络编程 1>什么是网络编程? 网络编程,是我们iOS程序开发者针对网络模块进行得代码编程,是作为一个资深开发...

  • IOS网络编程

    IOS网络编程 NSURLConnection NSURLSession是NSURLConnection 的替代者...

  • 网络相关以及TCP/IP协议

    一、iOS9 网络编程的重大改变: 1、网络请求方式的改变 1)NSURLConnection: iOS9之前使用...

  • iOS网络编程

    iOS网络相关类介绍 网络请求地址对象——NSURL url 介绍url,统一资源定位符,也被称为网址,因特网上标...

  • iOS网络编程

    网络编程 1. 概论 建立连接:通过IP或者域名来连接两台设备,通过端口号找到对应的通信程序 通信协议:要传输的数...

  • iOS网络编程

    一、URL URL的全称是Uniform Resource Locator(统一资源定位符),通过1个URL,能找...

网友评论

    本文标题:iOS网络编程(六)

    本文链接:https://www.haomeiwen.com/subject/hddsfxtx.html