美文网首页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网络编程(六)

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