2014-02-19 58 views
1

我有一个应用程序,我在其中打开一个PDF文件。无法下载并打开一个错误的PDF文件,无法在ios中查找PDF头

我成功地能够在一个webView中打开它。

现在我想下载该PDF文件并将其保存在我的文档文件夹中,并且希望在我的邮件中发送该PDF文件。

我搜索了很多,发现下面的最佳解决方案。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myfile.pdf"]; 

    if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ 

     NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.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]; 
    [webView setUserInteractionEnabled:YES]; 
    [webView setDelegate:self]; 
    webView.scalesPageToFit = YES; 
    [webView loadRequest:requestObj]; 

但是当我尝试这个代码,它给了我错误说未能找到PDF标题:'%PDF”未找到

我甚至无法打开我的PDF也。

我知道同样的问题,但我现在无法解决这个错误。

我不知道我在做什么错在这里。

抱歉给您带来不便。

请帮我这个。

回答

3

尝试下载&保存的PDF文件

UIActivityIndicatorView *indicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

    [indicator startAnimating]; 

    NSLog(@"Downloading Started"); 
    NSString *urlToDownload = @"http://gradcollege.okstate.edu/sites/default/files/PDF_linking.pdf"; 
    NSURL *url = [NSURL URLWithString:urlToDownload]; 
    NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:120.0f]; 

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

     [indicator stopAnimating]; 

     if (!connectionError) { 

      if (data) 
      { 
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
       NSString *documentsDirectory = [paths objectAtIndex:0]; 

       NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"myfile.pdf"]; 

       //saving is done on main thread 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [data 
        writeToFile:filePath atomically:YES]; 
        NSLog(@"File Saved !"); 

        NSURL *url = [NSURL fileURLWithPath:filePath]; 
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
        [_rssWebView setUserInteractionEnabled:YES]; 
        //[_rssWebView setDelegate:self]; 
        [_rssWebView loadRequest:requestObj]; 
       }); 
      } 
     } 

    }]; 
+0

感谢您的回复,你必须使用这个网址。但事情是文件被保存在文档目录路径中,并在webview中打开,但其大小仅为1Kb。并不是在那里开放。 – Manthan

+0

我已检查过。实际上您尝试下载的pdf链接受到ssl保护。 (将该链接粘贴到浏览器中,它将转换为https://)。我已经检查了一些随机的PDF链接,它工作正常。 – Pawan

+0

好的谢谢。你能给我提供任何正在工作的链接吗? – Manthan

0

如果转换为字符串的服务器响应,您在NSData *pdfData得到这个代码,你接下来会看到:

<!DOCTYPE html> 
<html> 
<head> 
    <title>iOS Developer Library</title> 
    <meta charset="utf-8"> 
    <script> 
     var content_page = "navigation"; 
     var href = window.location.href; 
     if (window.location.href.match('#')) { 
      var newhref = href.replace('#', ''); 
      newhref = newhref.replace('%23', '#'); 
      window.location.href = newhref; 
     } 
     else { 
      console.log(content_page); 
      console.log(content_page.match("content_page")); 
      if(content_page.match("content_page")) { 
       window.location.href = './navigation'; 
      } 
      else { 
       window.location.href = "./" + content_page; 
      } 
     } 
    </script> 
</head>  
<body> 
</body> 
<script type="text/javascript" src="/library/webstats/pagetracker.js"></script> 
<script type="text/javascript"> 
if(typeof PageTracker !== 'undefined') { 
    if(window.addEventListener) { 
    window.addEventListener("load", function(){PageTracker.logPageLoad()},false); 
    } else if(window.attachEvent) { 
    window.attachEvent("onload",function(){PageTracker.logPageLoad()}); 
    } 
} 
</script> 
</html> 

这只网页,正如我可以假设的那样,它会转发您使用另一个URI文档: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf。所以,如果你想下载.pdf文件

此外,上@爬完的回答要注意了,用他的方法,以避免下载文件在主线程