iOS 文件下载之断点下载Demo

使用NSMutableURLRequest请求网络

    1. HTTP请求头部指定长度范围
NSString *range = [NSString stringWithFormat:@"bytes=%zd-", self.currentLength];
     
 [request setValue:range forHTTPHeaderField:@"Range"];
    1. 创建一个空文件
[[NSFileManager defaultManager] createFileAtPath:self.filePath contents:nil attributes:nil];
    1. 创建一个文件指针
 NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.filePath];
#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UISlider *slider;

@property(nonatomic, assign)NSInteger countLength;

@property(nonatomic, assign)NSInteger currentLength;

@property(nonatomic, strong)NSString *filePath;

@property(nonatomic, strong)NSFileHandle *handle;

@property(nonatomic, strong)NSURLConnection *connec;

@end

@implementation ViewController

- (IBAction)start {
     
     [self downloadStart];
     
}

- (IBAction)pause {
     
     NSLog(@"%s", __func__);

     [self.connec cancel];
     
}

- (IBAction)resume {
     
     [self downloadStart];
     
}


- (void)downloadStart{
     
     NSLog(@"%s", __func__);
     
     NSURL *url = [NSURL URLWithString:@"http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/04/0E/ChMkJ1xG8LOISDE0AENk28OsmWwAAuhiwDQMkoAQ2Tz651.jpg?downfile=1551085025259.jpg"];
     
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
     
     NSString *range = [NSString stringWithFormat:@"bytes=%zd-", self.currentLength];
     
     [request setValue:range forHTTPHeaderField:@"Range"];
     
     self.connec = [[NSURLConnection alloc] initWithRequest:request delegate:self];
     
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
     
     NSLog(@"%s", __func__);
     
     if(self.countLength > 0){

          return;

     }
     
     self.countLength = response.expectedContentLength;
     
     self.filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"fengjing.jpg"];
     
     [[NSFileManager defaultManager] createFileAtPath:self.filePath contents:nil attributes:nil];
     
     NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.filePath];
     
     self.handle = handle;
     
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
     
     
     [self.handle seekToEndOfFile];
     
     [self.handle writeData:data];
     
     self.currentLength += data.length;
     
     CGFloat progress = 1.0 * self.currentLength / self.countLength;
     
     NSLog(@"当前下载进度:%.2f", progress);
     
     self.slider.value = progress;

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
     
     [self destroyHandle];

     NSLog(@"%s", __func__);
     
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
     
     [self destroyHandle];

     NSLog(@"%s", __func__);

     NSLog(@"%@", self.filePath);
     
}

- (void)destroyHandle{
     
     [self.handle closeFile];

     self.handle = nil;
     
}

@end

你可能感兴趣的:(iOS 文件下载之断点下载Demo)