2014-02-19 94 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文件

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