2011-11-03 86 views
2

我正在構建一個可以保存電子郵件附件的iPhone應用程序。我希望能夠從應用程序內部查看文件。我需要能夠查看盡可能多的文件類型:.txt,.pdf,.png,.jpeg,.doc,.xls等。如何查看iPhone應用程序中的文件?

這些文件將駐留在我的應用程序的文檔文件夾。什麼是最好的解決方案?我的第一個想法是將我的文件路徑轉換爲URL並將它們加載到UIWebView中,但我不確定這是最好的方式還是處理所有這些文件類型。有什麼想法嗎?

回答

2

蘋果自己的郵件應用程序這樣做的方式是與QuickLook框架。我建議你也這樣做。 QLPreviewController爲你做了所有繁重的工作。你也可以使用UIDocumentInteractionController;這仍然在後臺使用QuickLook來生成預覽。

+0

感謝您的建議。我給了這個鏡頭,但是當控制器啓動時,我什麼都看不到,並得到這個警告: 「警告:無法讀取符號/Developer/Platforms/iPhoneOS.platform/DeviceSupport/5.0(9A334)/ Symbols/System/Library /Frameworks/QuickLook.framework/DisplayBundles/Image.qldisplay/Image(找不到文件) 警告:在本地找到Image.qldisplay/Image的副本,從遠程設備的內存中讀取,這可能會降低調試會話的速度。有什麼建議麼? – user1007895

+0

@user:工作示例https://github.com/mattneub/Programming-iOS-4-Book-Examples/tree/master/p736p754fileHandoff – matt

+0

@matt鏈接已移動,現在正確的鏈接是:[https:/ /github.com/mattneub/Programming-iOS-Book-Examples](https://github.com/mattneub/Programming-iOS-Book-Examples)。 – zaph

2

UIWebView是顯示這種文件的最佳方式。

或者你可以嘗試使用此代碼讀取文件中的其他應用程序:

 UIDocumentInteractionController *interactionController = [[UIDocumentInteractionController interactionControllerWithURL:document.documentURL] retain]; 
     interactionController.delegate = self; 
     BOOL canOpenDocument = [interactionController presentOpenInMenuFromRect:CGRectMake(365, 200, 300, 400) inView:self.view animated:YES]; 

     if(!canOpenDocument) { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Your phone can't read this kind of file, please install an app from appstore for that" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [alert show]; 
      [alert release]; 
     } 
+0

如果他想要做的只是顯示預覽,如果QuickLook已經知道如何顯示它,將文檔交給另一個應用程序是很愚蠢的。而不是使用UIDocumentInteractionController將文檔交給另一個應用程序,他應該使用UIDocumentInteractionController來保持他的位置並顯示預覽!這實際上與使用QLPreviewController相同。 – matt

相關問題