ios – 将本地PDF文件加载到WebView中

前端之家收集整理的这篇文章主要介绍了ios – 将本地PDF文件加载到WebView中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_3@
我试图将以下功能放入我正在编写的iOS应用程序中:

>在XCode中的项目的资源文件夹中发送一组PDF
>将PDF复制到app目录
>在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];
@H_403_3@

猜你在找的iOS相关文章