我制作了一个可以创建 pdf 并将其存储在应用程序文档文件夹中的应用程序。我现在想打开它并在按下“查看 pdf”UIButton
时从应用程序中查看它。
我已经看过这里的几个问题,我考虑过单独的 View Controller 或 ScrollView 。
最好的方法是什么?
更新:
我听从了建议,正在尝试使用 QLPreviewController
。我已经添加了 QuickLook
框架,现在有了以下内容,但我仍然不知道如何在 pathForResource
中识别路径。有什么建议吗?
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];
return [NSURL fileURLWithPath:path];
}
- (IBAction)viewPdfButton:(id)sender {
NSString *filename= @"ObservationPDF.pdf";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documnetDirectory = [path objectAtIndex:0];
NSString *pdfPathWithFileName = [documnetDirectory stringByAppendingPathComponent:filename];
[self generatePdf: pdfPathWithFileName];
QLPreviewController *previewController=[[QLPreviewController alloc]init];
previewController.delegate=self;
previewController.dataSource=self;
[self presentViewController:previewController animated:YES completion:nil];
最佳答案
如果 PDF 文件位于应用程序文档文件夹中,那么您不应该考虑将其传递给另一个应用程序,您应该考虑在应用程序中显示该文件。 2 个常规选项:
- 添加一个
UIWebView
并将本地文件载入其中 - 使用
QLPreviewController
显示包含 PDF 的新 View
Web View 很简单,不需要在 UI 上进行转换。预览 Controller 需要转换,但免费提供一些共享/打印支持。
这一行很困惑(从外观上看是无效的语法):
NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];
您仅使用 NSBundle
从 bundle 中获取项目,而这不是您所拥有的。您应该只使用保存文件的文件路径创建 URL:
[NSURL fileURLWithPath:pdfPathWithFileName];
(您可以存储它,或者您可能需要以与保存文件时相同的方式重新创建)
关于ios - 在 Xcode 项目中打开保存在我的文档文件夹中的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21382011/