2011-09-21 133 views
4

我試圖把下面的功能集成到iOS應用程序我寫:加載本地PDF文件到的WebView

  • 船舶一組項目的資源文件夾中的PDF文件的XCode中
  • 複製PDF到應用程序目錄
  • 在webview中打開PDF。

據我所見,前兩步工作正常(我已經使用FileManager在複製操作後檢查fileExistsAtPath)。 但是,web視圖爲空,並且正在出錯(「請求的URL在服務器上不存在」)。 我的文件打開代碼如下:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES); 
NSString *localDocumentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfFileName = @"example.pdf"; 
NSString *localDocumentsDirectoryPdfFilePath = [localDocumentsDirectory 
            stringByAppendingPathComponent:pdfFileName]; 
pdfUrl = [NSURL fileURLWithPath:localDocumentsDirectoryPdfFilePath]; 
[webView loadRequest:[NSURLRequestWithURL:pdfUrl]; 

也能正常工作在模擬器上,但

+4

該設備是區分大小寫的。確保文件名完全匹配。 – Anna

+0

安娜,謝謝! 我剛纔說我已經檢查過了。但是當我回去再看時,.pdf是事實上的.PDF 如果您將此作爲答案添加,我會將其標記爲已解決。 再次感謝。 – HaemEternal

回答

0

張貼由安娜·卡列尼娜以上,「該設備是區分大小寫的。請確保文件名完全一致」

1

你確定你不想讓UIDocumentInteractionController做設備上不工作爲你舉重嗎?

UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; 
dc.delegate = self; 
[dc presentPreviewAnimated:YES]; 
+0

感謝您的建議。 我以前沒見過,我會試試看。 – HaemEternal

+0

很好的回答!爲Swift 3添加一個答案 –

0

由於bshirley建議UIDocumentInteractionController是提供您的PDF一個很好的選擇。最初我嘗試使用第三方JSQWebViewController但我在模擬器上運行時收到設備上的空白屏幕。 UIDocumentInteractionController對我很好!對於斯威夫特你可以這樣做:

let interactionController = UIDocumentInteractionController(url: fileURL) 
interactionController.delegate = self 
interactionController.presentPreview(animated: true) 

和實施委託方法:

// Mark: UIDocumentInteractionControllerDelegate 
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { 
    return UIApplication.shared.keyWindow!.rootViewController! 
} 
相關問題