ios 使用NSURlSession 下载文件并且使用进度条进行文件下载进度读取以及打开附件(word,excel,ppt,pdf)显示


ios 使用NSURlSession 下载文件并且使用进度条进行文件下载进度读取以及打开附件(word,excel,ppt,pdf)显示

话不多说,直接写代码了

    NSURLSession * session = [NSURLSessionsessionWithConfiguration:[NSURLSessionConfigurationdefaultSessionConfiguration]delegate:selfdelegateQueue:[NSOperationQueuemainQueue]];

    NSURL * url = [NSURLURLWithString:_filleDic[@"fileUrl"]];

    NSURLSessionDownloadTask * downLoadRask = [sessiondownloadTaskWithURL:url];

    [downLoadRaskresume];//开启一个resume



-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{

    progressView.progress =1.0*totalBytesWritten/[_filleDic[@"fileSize"]longLongValue];

    longlong fileSize = [_filleDic[@"fileSize"]longLongValue];

    progressDetailLab.text = [NSStringstringWithFormat:@"正在下载:%.2fk/%.2fk",totalBytesWritten/1024.0,fileSize/1024.0];// 1024 1MB包含的字节

    progressDetailLab.textColor = [UIColorcolorWithHexString:@"#999999"];

    [progressViewsetProgress:progressView.progressanimated:YES];

    

}

//重新恢复下载的代理方法//断点下载使用,目前未使用

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{



}

//写入数据到本地的时候调用的方法

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{

    //此处得到存放文件的路径

    NSString * fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:_filleDic[@"fileName"]];

    [[NSFileManagerdefaultManager]moveItemAtURL:locationtoURL:[NSURLfileURLWithPath:fullPath]error:nil];

    _filePath = fullPath;

    QLPreviewController * qlpc = [[QLPreviewControlleralloc]init];//QLPreviewController打开附近的苹果原生的类库

    qlpc.dataSource =self;

    [selfpresentViewController:qlpcanimated:YEScompletion:nil];


}

//请求完成,错误调用的代理方法,下载失败的时候重新下载

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{

    if (error) {

        [selfshowHubTitle:self.viewtitleText:@"文件下载失败,请检查网络"location:HEIGHT/2-60];

//请求错误之后会创建一个button,点击后可以重新下载

        UIButton *downLoadBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [downLoadBtnsetTitle:@"重新下载"forState:UIControlStateNormal];

        downLoadBtn.backgroundColor = [UIColorcolorWithHexString:@"#4ea375"];

        downLoadBtn.layer.cornerRadius =3;

        downLoadBtn.layer.masksToBounds =YES;

        [self.viewaddSubview:downLoadBtn];

        [downLoadBtnaddTarget:selfaction:@selector(downLoadBtnClick:)forControlEvents:UIControlEventTouchUpInside];

        [downLoadBtnmas_makeConstraints:^(MASConstraintMaker *make) {

            make.bottom.equalTo(self.view.mas_bottom).offset(-55);

            make.top.equalTo(self.view.mas_bottom).offset(-100);

            make.centerX.equalTo(self.view.mas_centerX);

            make.size.mas_equalTo(CGSizeMake(120,45));

        }];

    }

}

-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{

    return1;

}

//直接得到文档的url并打开

-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{


    return [NSURLfileURLWithPath:_filePath];


}


不过使用QLViewController 苹果原声自带的分享按钮无法自定义,曾经尝试让SysNoticeAttathViewController继承与QlViewController来解决但是自定义的右侧navgationbar.rightitem在iPhone 6p上面还是会显示,所以使用继承并无法根本上改变问题

所以只能addSubView:qlView.subview来解决,不加载发现按钮的视图,但是只有6p是好的,其他型号的手机还是会有苹果自带的分享按钮,因此改变策略使用webview加载



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值