iOS文档下载功能

/**
下载文档
*/

- (IBAction)xiazai:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths lastObject];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:self.filename]; 

        if ([fileManager fileExistsAtPath:filePath]) {
            //文件已经存在,直接打开
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"是否打开文件?" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * cancelAction  =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
            [alertController addAction:cancelAction];
    
            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [self openDocxWithPath:filePath];
            }]];
    
            [self presentViewController:alertController animated:YES completion:nil];
    
        }else {
            //文件不存在,要下载
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"是否下载并打开文件?" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * cancelAction  =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
            [alertController addAction:cancelAction];
    
            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [self downloadDocxWithDocPath:documentsDirectory fileName:self.filename];
            }]];
    
            [self presentViewController:alertController animated:YES completion:nil];
    
        }
}
-(void)downloadDocxWithDocPath:(NSString *)docPath fileName:(NSString *)fileName{
        [SVProgressHUD show];
    //创建 Request
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"下载进度:%f",downloadProgress.fractionCompleted);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        [SVProgressHUD dismiss];
        NSString *path = [docPath stringByAppendingPathComponent:fileName];
        return [NSURL fileURLWithPath:path];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        NSString *name = [filePath path];
        [self openDocxWithPath:name];
        
    }];
    [task resume];//开始下载 要不然不会进行下载的
    
}
-(void)openDocxWithPath:(NSString *)filePath {
   //我是用网页打开的
    SafariVC *vc = [[SafariVC alloc] init];
    vc.url = self.url;
    [self push:vc];
 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值