2012-02-20 62 views
1

目前,我通过检查当前的URL来检测UIWebView是否加载PDF文件。接下来我用ASIHTTPRequest库下载pdf文件。问题是,如果文件显示在UIWebView中,它是否已经下载到某处,所以我下载了这个文件两次。我怎样才能得到这个文件加载在我的UIWebView?如何将pdf文件加载到我的Documents目录中的UIWebView中?

目的是将此文件加载到我的UIWebView中加载到我的Document目录中。

+0

Duplicate:http://stackoverflow.com/questions/2832245/iphone-can-we-open-pdf-file-using-uiwebview – Uko 2012-02-20 06:59:31

+0

是不是同一个问题,我想存储一个pdf已经加载在我的文档目录中,不在UIWebview中加载它。 – Anthony 2012-02-20 07:06:12

+0

你想自动执行还是当用户提问?因为不是从网上显示PDF,您可以下载并存储它,并在Web视图中显示它。根据http://www.iphonedevsdk.com/forum/iphone-sdk-development/5608-uiwebview-how-get-content-shown.html#post333645 pdf数据无法从Web视图获得 – Uko 2012-02-20 07:21:51

回答

1

这里是你如何下载,阅读和本地存储PDF的iPhone应用程序,让你不必定期下载:

首先创建一个UIWebView并包括< < UIWebViewDelegate >>

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ // if file not present 
      // download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link 
     NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]]; 
      //Store the downloaded file in documents directory as a NSData format 
     [pdfData writeToFile:filePath atomically:YES]; 
    } 

    NSURL *url = [NSURL fileURLWithPath:filePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [yourWebView setUserInteractionEnabled:YES]; 
    [yourWebView setDelegate:self]; 
    yourWebView.scalesPageToFit = YES; 
    [yourWebView loadRequest:requestObj]; 

或者,如果你只是想从你的资源文件夹读/加载PDF然后简单地做到这一点:

 NSString* filePath= [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"]]; 
    /// or you can even read docs file as : pathForResource:@"sample" ofType:@"docx"] 
    NSURL *url = [NSURL fileURLWithPath:filePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [yourWebView setUserInteractionEnabled:YES]; 
    [yourWebView setDelegate:self]; 
    yourWebView.scalesPageToFit = YES; 
    [yourWebView loadRequest:requestObj]; 
+0

谢谢,它的工作原理。 – Bhimbim 2017-08-15 08:42:33

0

我只能再次下载它,或者把它放在临时存储器中,然后把它放在UIWebView中,当用户询问时,然后将它放到临时存储器所需的位置。

相关问题