我正在构建一个可以保存电子邮件附件的iPhone应用程序。我希望能够从应用程序内部查看文件。我需要能够查看尽可能多的文件类型:.txt,.pdf,.png,.jpeg,.doc,.xls等。如何查看iPhone应用程序中的文件?
这些文件将驻留在我的应用程序的文档文件夹。什么是最好的解决方案?我的第一个想法是将我的文件路径转换为URL并将它们加载到UIWebView中,但我不确定这是最好的方式还是处理所有这些文件类型。有什么想法吗?
我正在构建一个可以保存电子邮件附件的iPhone应用程序。我希望能够从应用程序内部查看文件。我需要能够查看尽可能多的文件类型:.txt,.pdf,.png,.jpeg,.doc,.xls等。如何查看iPhone应用程序中的文件?
这些文件将驻留在我的应用程序的文档文件夹。什么是最好的解决方案?我的第一个想法是将我的文件路径转换为URL并将它们加载到UIWebView中,但我不确定这是最好的方式还是处理所有这些文件类型。有什么想法吗?
苹果自己的邮件应用程序这样做的方式是与QuickLook框架。我建议你也这样做。 QLPreviewController为你做了所有繁重的工作。你也可以使用UIDocumentInteractionController;这仍然在后台使用QuickLook来生成预览。
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];
}
如果他想要做的只是显示预览,如果QuickLook已经知道如何显示它,将文档交给另一个应用程序是很愚蠢的。而不是使用UIDocumentInteractionController将文档交给另一个应用程序,他应该使用UIDocumentInteractionController来保持他的位置并显示预览!这实际上与使用QLPreviewController相同。 – matt
感谢您的建议。我给了这个镜头,但是当控制器启动时,我什么都看不到,并得到这个警告: “警告:无法读取符号/Developer/Platforms/iPhoneOS.platform/DeviceSupport/5.0(9A334)/ Symbols/System/Library /Frameworks/QuickLook.framework/DisplayBundles/Image.qldisplay/Image(找不到文件) 警告:在本地找到Image.qldisplay/Image的副本,从远程设备的内存中读取,这可能会降低调试会话的速度。有什么建议么? – user1007895
@user:工作示例https://github.com/mattneub/Programming-iOS-4-Book-Examples/tree/master/p736p754fileHandoff – matt
@matt链接已移动,现在正确的链接是:[https:/ /github.com/mattneub/Programming-iOS-Book-Examples](https://github.com/mattneub/Programming-iOS-Book-Examples)。 – zaph