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];

    

}

 

 

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

 

 

這其中沒有做斷點續傳,日后有遇到再更新!  歡迎提出和指正


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM