iOS:在文档应用程序中下载并保存 PDF 文件

标签 ios objective-c pdf uiwebview

我想下载一个 PDF 文件,然后在 webView 中打开它。我搜索了一下然后找到了这个 issu .接受的答案描述了如何下载和显示文件。当我测试它时,我无法在文档中找到该文件,因为它存储在我的应用程序中。此外,我不认为该文件已保存,因为即使我传递要加载的文件路径,pdf 也永远不会显示在 webView 中。这是答案的代码:

// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
    NSURL URLWithString:@"http://www.example.com/info.pdf"]];

// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
    [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
        stringByAppendingPathComponent:@"Documents"
]];

NSString *filePath = [resourceDocPath 
    stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];


// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];

这是我在日志中看到的:

libMobileGestalt MobileGestaltSupport.m:153: pid 2828 (my_app) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled
2017-08-01 10:29:06.882562+0100 my_app[2828:1334057] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see <rdar://problem/11744455>)

我做的有什么问题?我怎样才能将下载的文件放入我的 iPhoneDocuments 应用程序中,以便用户像任何其他 PDF 一样随时进行检查?

最佳答案

沙箱路径错误

NSString *resourceDocPath = [[NSString alloc] initWithString:[
    [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
        stringByAppendingPathComponent:@"Documents"
]];

请使用您的沙箱路径而不是包路径。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

关于iOS:在文档应用程序中下载并保存 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45434283/

相关文章:

objective-c - 错误:指针不兼容(数据未传递到另一个ViewController)

ios - "Bad Realm file header (#1)"iOS打开Realm文件异常

iphone - 将数据保存到存储器

ios - 如何为 self.typeOfAssetLabel.text 进行 KVC

ios - 如何使用 Swift 3 自定义 googlemap infoWindow

ios - 表格单元格中的 UIswitch 重复问题

iphone - 多个字典的排序数组

android - 下载 PDF 时 InputStream 返回空文件

python - PyPDF2==1.26.0 文本提取不适用于某些 pdf

java - 在不打开 PDF 的情况下在客户端浏览器打印 PDF