【问题标题】:How to store a pdf file loaded into an UIWebView in my Documents directory?如何在我的 Documents 目录中存储加载到 UIWebView 中的 pdf 文件?
【发布时间】:2012-03-10 13:36:42
【问题描述】:

目前,我通过检查当前 URL 来检测 UIWebView 是否加载 pdf 文件。接下来我下载带有 ASIHTTPRequest 库的 pdf 文件。问题是,如果文件显示在 UIWebView 中,是不是已经下载到某个地方,所以我下载了这个文件两次。如何在我的 UIWebView 中加载此文件?

目的是将我的UIWebView中加载的这个文件存储在我的Document目录中。

【问题讨论】:

标签: objective-c ios uiwebview


【解决方案1】:

您可以通过以下方式在 iphone 应用程序中本地下载、阅读和存储您的 pdf,这样您就不必经常下载:

首先创建 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];

【讨论】:

    【解决方案2】:

    我只能建议第二次下载它,或者将其放入临时存储中,然后将其放入 UIWebView,当用户询问时,将其从临时存储中放到您想要的位置。

    【讨论】:

      猜你喜欢
      • 2013-11-04
      • 2017-02-13
      • 2012-11-02
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多