iOS 网络篇--PDF网络文件下载和打开

  最近的项目要用到一个在线报告的下载,于是完成后自己在理一下思路,大体的实现了我要得需求。

话不多说,直接上代码 

首先,取到网络文件的链接,进行判段是否需求再次下载还是直接打开

#pragma mark   下载报告

////     第一步

//是否下载还是打开文件

- (void)downloadPDF:(NSString *)downloadUrl{

    NSArray *array = [downloadUrl componentsSeparatedByString:@"/"]; //从字符/中分隔成多个元素的数组

    NSString *file = [array lastObject];

    

    if ([self isFileExist:file]) {

        

        //获取Documents 下的文件路径

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *path = [paths objectAtIndex:0];

        NSString *pathString = [path stringByAppendingFormat:@"/%@",file];

        NSLog(@"path:%@", pathString);

        [self loadDocument:pathString];

    }else{

        //从新下载

        [self downloadFile:downloadUrl];

    }

    

}

 

#pragma mark      第二步    判断沙盒中是否存在此文件

 

-(BOOL) isFileExist:(NSString *)fileName

{

    //获取Documents 下的文件路径

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path = [paths objectAtIndex:0];

    NSString *filePath = [path stringByAppendingPathComponent:fileName];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL result = [fileManager fileExistsAtPath:filePath];

    NSLog(@"这个文件已经存在:%@",result?@"是的":@"不存在");

    return result;

}

 

 

//////////    第三步

//下载PDF

- (void)downloadFile:(NSString *)downLoadUrl{

    

    __weak typeof(self)weakSelf = self;

    

    [self hudTipWillShow:YES];

    [DataService downloadTaskWithURL:downLoadUrl completion:^(id result) {

        NSLog(@"%@",result);

        

        NSProgress *downloadProgress = result;

        

        if (weakSelf.HUD) {

            

            weakSelf.HUD.progress = downloadProgress.fractionCompleted;

            

            _HUD.labelText = [NSString stringWithFormat:@"%2.f%%",downloadProgress.fractionCompleted*100];

            

        }

        

    } filesPath:^(id filesPath) {

        

        [_rePortDwn setBackgroundImage:[UIImage imageNamed:@"downLoad"] forState:UIControlStateNormal];

        //        NSLog(@"%@",filesPath);

        NSURL*urlString = filesPath;

        NSString *string = [urlString absoluteString];

        NSArray *array = [string componentsSeparatedByString:@"/"]; //从字符A中分隔成2个元素的数组

        NSString *file = [array lastObject];

        NSLog(@"filePathName = :%@",file);

        [weakSelf hudTipWillShow:NO];

        

    }];

    

}

 

 

///////       第四步

//已经下载了的文件用webview显示

-(void)loadDocument:(NSString *)documentName

{

    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, kSCREEN_WIDTH, kSCREEN_HEIGHT)];

    [self.view addSubview:webView];

    NSURL *url = [NSURL fileURLWithPath:documentName];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [webView loadRequest:request];

    

}

 

 

最后,直接调用第一步的方法就可以了。  

 

 

这其中没有做断点续传,日后有遇到再更新!  欢迎提出和指正

posted @ 2017-04-06 14:27  猿咧  阅读(6235)  评论(2编辑  收藏  举报