iOS中断点续传(随心所欲下载缓存文件到本地)

iOS中的断点续传,对于做视频类或下载的来说,比较重要,因为对于大文件,我们不可能迅速下载完成,这时候就需要我们本地将已经下载的给保存下来。
在此之前,网上流传的大多是是NSURLConnection或者NSURLSessionDownloadTask这两种方法来进行我们的需求,前者好像在iOS8左右就已经被苹果抛弃了( 反正iOS9已被废弃),后者用到的方法在下载的过程中,它并不会保存到本地,只会等到下载完成后,才会在本地显示(当然了,你也可以用文件读写的方式将他的缓存文件自动写入到本地),网上关于这两种方法的demo我都研究了,简单归简单,但是效果要么不好,要么就是被废弃了,不能将就,得另寻办法~
之后在网上辗转,发现了一个大侠自己封装的一个断点续传HSDownloadManager,他在内部用了NSURLSessionDataTask以及NSOutputStream流的方式,实现了可以多文件下载,并能同步缓存到本地,大家可以去github上下载: https://github.com/HHuiHao/HSDownloadManager

当然了,大虾写的也有bug,看了一下午他的内部实现原理,发现他的demo里,第一行删除时候,任务队列虽然删除了,但是并没有停止任务,导致了session的代理方法依然走,导致点击清空时候程序会crash (我看网上也有些朋友提到过这个第三方,可都没发现~),所以需要改一下。
具体内部实现在这不提了,有兴趣的朋友下载这个第三方后(把那个bug改掉),可以自行查看,内部实现不难,一个单例,几个方法。在这里提一下怎么简单使用(快速集成到项目里),我自己写了个简单的demo,具体如下

1.将HSDownloadManager导入到项目里

2.定义基本控件属性

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *beginBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

3.viewdidload中的简单控件代码

NSString * const downloadUrl = @"http://120.25.226.186:32812/resources/videos/minion_01.mp4";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"沙盒路径: %@",NSHomeDirectory());

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"断点续传";

    _beginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _beginBtn.frame = CGRectMake(100, 100, 80, 30);
    [_beginBtn setTitle:[self getTitleWithDownloadState:DownloadStateSuspended] forState:UIControlStateNormal];
    
    _beginBtn.backgroundColor = [UIColor cyanColor];
    [_beginBtn addTarget:self action:@selector(handleBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_beginBtn];
    
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelBtn setTitle:@"删除" forState:UIControlStateNormal];
    _cancelBtn.frame = CGRectMake(100, 300, 80, 30);
    _cancelBtn.backgroundColor = [UIColor cyanColor];
    [_cancelBtn addTarget:self action:@selector(cancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_cancelBtn];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 150, 260, 20)];
    [self.view addSubview:_titleLabel];
    _titleLabel.text = @"当前下载进度:";
    _titleLabel.textAlignment = 0;
    [self refreshDataWithState:DownloadStateSuspended];
    
}

4.定义两个方法,分别是刷新数据以及按钮状态的

#pragma mark 刷新数据(用来判断程序刚进来时候下载的文件本地是否存在过,进度咋样)
- (void)refreshDataWithState:(DownloadState)state
{
    _titleLabel.text = [NSString stringWithFormat:@"当前下载进度:%.f%%", [[HSDownloadManager sharedInstance] progress:downloadUrl] * 100];

    [self.beginBtn setTitle:[self getTitleWithDownloadState:state] forState:UIControlStateNormal];
    

    NSLog(@"-----%f", [[HSDownloadManager sharedInstance] progress:downloadUrl]);
}

#pragma mark 按钮状态
- (NSString *)getTitleWithDownloadState:(DownloadState)state
{
    switch (state) {
        case DownloadStateStart:
            return @"暂停";
        case DownloadStateSuspended:
        case DownloadStateFailed:
            return @"开始";
        case DownloadStateCompleted:
            return @"完成";
        default:
            break;
    }
}

5.最后点击方法以及删除方法

- (void)handleBtn:(UIButton *)btn {

    [[HSDownloadManager sharedInstance] download:downloadUrl progress:^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            _titleLabel.text = [NSString stringWithFormat:@"当前下载进度:%.2f%%", progress* 100];
        });
        
    } state:^(DownloadState state) {
        NSLog(@"dsfsdgsd");
        NSLog(@"state == %u", state);
        dispatch_async(dispatch_get_main_queue(), ^{
            [btn setTitle:[self getTitleWithDownloadState:state] forState:UIControlStateNormal];
        });
    }];
    
}

- (void)cancelBtn:(UIButton *)btn {
    
    [[HSDownloadManager sharedInstance] deleteFile:downloadUrl];
    _titleLabel.text = [NSString stringWithFormat:@"当前下载进度:%.f%%", [[HSDownloadManager sharedInstance] progress:downloadUrl] * 100];
}

效果如下:

iOS中断点续传(随心所欲下载缓存文件到本地)_第1张图片
session.gif

怎么样,是不是挺好使的~

如果你喜欢的话,就点个赞吧

你可能感兴趣的:(iOS中断点续传(随心所欲下载缓存文件到本地))