2015-01-07 65 views
0

我使用的Xcode 6.1.1店從內部網絡PDF格式,並顯示到的WebView

開發一個應用程序適用於iOS 8.1和我想下載從互聯網上一個PDF,店到我的iPad,然後加載它成網頁視圖;這是PDF測試:

pdf screenshot

這是結果我得到在iPad上嘗試顯示它在我的WebView後:

iPad screenshot

這是到目前爲止我的代碼...

// Get the PDF Data from the url in a NSData Object 
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]]; 

    // Store the Data locally as PDF File 
    NSArray *searchPaths   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentFolderPath = searchPaths[0]; 
    NSString *archivePath   = [documentFolderPath stringByAppendingPathComponent:@"larg-file.pdf"]; 

    [pdfData writeToFile:archivePath atomically:YES]; 

    // Now create Request for the file that was saved in your documents folder 
    NSURL *url = [NSURL fileURLWithPath:archivePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [_webView setUserInteractionEnabled:YES]; 
    [_webView setDelegate:self]; 
    [_webView loadRequest:requestObj]; 

我該如何做這項工作?

編輯:這就是答案

NSArray *searchPaths   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentFolderPath = searchPaths[0]; 
    NSString *archivePath   = [documentFolderPath stringByAppendingPathComponent:@"larg-file.pdf"]; 

    if (![Functions isAlreadyStoredinDeviceFileWithName:@"larg-file.pdf"]) { 

     NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]]; 
     [pdfData writeToFile:archivePath atomically:YES]; 
    } 


    // Now create Request for the file that was saved in your documents folder 
    NSURL *url     = [NSURL fileURLWithPath:archivePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [_webView setUserInteractionEnabled:YES]; 
    [_webView setDelegate:self]; 
    [_webView loadRequest:requestObj]; 
+0

你如何在Webview中加載? –

+1

爲什麼使用網絡視圖?爲什麼不使用'QLPreviewController'? – rmaddy

+0

僅供參考 - 您構建「文檔」路徑的邏輯錯誤,並且在iOS 8下無法運行。 – rmaddy

回答

1

此代碼:

NSString *resourceDocPath = [[NSString alloc] initWithString:[ 
    [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] 
    stringByAppendingPathComponent:@"Documents"]]; 

試圖制定應用程序包中的目錄。您沒有對應用程序包的寫入權限。因此您的文件不會被保存。

查看-[NSFileManager URLsForDirectory:inDomains:]NSSearchPathForDirectoriesInDomains瞭解如何獲取用戶文檔文件夾的路徑。

+0

終於工作謝謝 – Jesus