iOS好使的各种文件文档、音乐、视频下载查看器有进度条

一、使用说明

采用第三方下载插件MCDownloadManager,

csdn地址   http://download.csdn.net/download/wangxiaoertedaye/10166107

github地址  https://github.com/zxgiOS/MCDownloadManager

两种实现方式

1)分步式,点击下载然后点击打开,可以使用按钮点击下载、查看,

2)一体式,点击直接下载打开,点击之后将URL传入插件调用

     MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:self.url];

    根据receipt.state判断是否下载或者直接打开,如果返回已下载就直接调用下述打开方法,未下载则调用下载方法下载完成后打开

二、效果图


三、使用方法

1.设置下载URL,设置进度条
    MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:url];
    self.progressView.progress = receipt.progress.fractionCompleted;
2.点击开始下载,download中监听下载进度更新进度条
    - (void)downloadBtnAction:(UIButton *)sender{
    
    MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:self.url];
    
    if (receipt.state == MCDownloadStateDownloading) {//当前正在下载中
        [sender setTitle:@"下载" forState:UIControlStateNormal];
        [[MCDownloadManager defaultInstance] suspendWithDownloadReceipt:receipt];
    }else if (receipt.state == MCDownloadStateCompleted) {//已下载
        
        if ([self.delegate respondsToSelector:@selector(cell:didClickedBtn:)]) {
            [self.delegate cell:self didClickedBtn:sender];
        }
    }else {//未下载
        [sender setTitle:@"停止" forState:UIControlStateNormal];
        [self download];
    }

}
- (void)download {
    [[MCDownloadManager defaultInstance] downloadFileWithURL:self.url
                                                    progress:^(NSProgress * _Nonnull downloadProgress, MCDownloadReceipt *receipt) {
                                                        NSLog(@"%@,%@",receipt.url,self.url);
                                                        if ([receipt.url isEqualToString:self.url]) {
                                                            self.progressView.progress = downloadProgress.fractionCompleted ;
                                                            self.sizeLabel.text = [NSString stringWithFormat:@"%0.2fm/%0.2fm", downloadProgress.completedUnitCount/1024.0/1024, downloadProgress.totalUnitCount/1024.0/1024];
                                                        }
                                                        
                                                    }
                                                 destination:nil
                                                     success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSURL * _Nonnull filePath) {
                                                         [self.downloadBtn setTitle:@"查看" forState:UIControlStateNormal];
                                                     }
                                                     failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
                                                         [self.downloadBtn setTitle:@"下载" forState:UIControlStateNormal];
                                                     }];
    
}
3.下载完成查看  传入需要查看文件的URL 插件自动关联已经下载好的(相当于缓存机制,避免重复下载)如果需要下载之后直接打开则在上述download方法下载完成中调用打开方法

- (void)openDocBycell:(DownLoadTableViewCell *)cell{
    
    MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:cell.url];
    //UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    self.doc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:receipt.filePath]];
    
    self.doc.name = @"文档预览";
    self.doc.accessibilityNavigationStyle = 2;
    self.doc.delegate = self;
    [self.doc presentPreviewAnimated:YES];
    //[documentVC openFileWithURL:[NSURL fileURLWithPath:receipt.filePath]];
}

猜你喜欢

转载自blog.csdn.net/wangxiaoertedaye/article/details/78852896
今日推荐