美文网首页iOS Code
iOS AFNetworking 3.0 下载附件(doc,ex

iOS AFNetworking 3.0 下载附件(doc,ex

作者: 字母大师 | 来源:发表于2017-11-21 11:34 被阅读20次

很多项目中都会遇到附件下载功能,尤其工具类APP ,这篇文章简单介绍了下载预览功能

下载功能

/**
 下载附件
 @param fileName 文件名
 */
-(void)downloadFileWithFileName:(NSString *)fileName {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    /* 创建网络下载对象 */
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
        NSString *urlString = imageBaseURL;
    
        urlString = [urlString stringByAppendingString:fileName];
    /* 下载地址 */
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    /* 下载路径 */
    NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSString *filePath = [path stringByAppendingPathComponent:url.lastPathComponent];
    
    /* 开始请求下载 */
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
        DebugLog(@"下载量:%.0f%", downloadProgress.fractionCompleted * 100);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
        /* 设定下载到的位置 */
        return [NSURL fileURLWithPath:filePath];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showInView:self.view tip:@"下载完成,正在打开" autoHid:YES];
        DebugLog(@"下载完成");
        DebugLog(@"%@",filePath);
        NSString *name = [filePath path];
          [self openFileWithPath:name];
    }];
    [downloadTask resume];
}

预览

楼主用的WKWebView,当然还有其他办法

    WKWebViewViewController *momentVC = [[WKWebViewViewController alloc]init];
    momentVC.urlString = filePath;
    momentVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController: momentVC animated:NO];


  url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.urlString]];

点击下载按钮判断是否已经下载过了

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths lastObject];
            
            NSFileManager *fileManager = [NSFileManager defaultManager];
            
            /* 下载路径 */
            NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
            NSString *filePath = [path stringByAppendingPathComponent: vc.ulr.lastPathComponent];
            
            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 openFileWithPath:filePath];
                }]];
                
                [alertController.actions setValue:[UIColor colorWithHexString:@"3998ef"] forKey:@"_titleTextColor"];
                [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 downloadFileWithDocPath:documentsDirectory fileName: self.strUrl];
                }]];
                [self presentViewController:alertController animated:YES completion:nil];

相关文章

网友评论

    本文标题:iOS AFNetworking 3.0 下载附件(doc,ex

    本文链接:https://www.haomeiwen.com/subject/bfwivxtx.html