我正在尝试将以下功能放入我正在编写的 iOS 应用程序中:
- 在 XCode 项目的资源文件夹中发送一组 PDF
- 将 PDF 复制到应用程序目录
- 在 WebView 中打开 PDF。
据我所知,前两个步骤工作正常(复制操作后我使用 FileManager 检查 fileExistsAtPath)。 但是,webview 是空的,并且出现错误(“请求的 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];
这在模拟器上工作正常,但在设备上不工作
最佳答案
您确定不想让UIDocumentInteractionController为你做繁重的工作?
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
关于ios - 将本地 PDF 文件加载到 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7502189/