【问题标题】:iOS: Download and save PDF file in Documents appiOS:在 Documents 应用程序中下载并保存 PDF 文件
【发布时间】:2018-01-08 02:40:51
【问题描述】:

我想下载一个 PDF 文件,然后在 webView 中打开它。我搜索了一下,然后找到了这个issu。接受的答案描述了如何下载和显示文件。当我测试它时,我在 Documents 中找不到该文件,因为它存储在我的应用程序中。此外,我不认为该文件已保存,因为即使我传递要加载的 filePath,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 一样进行检查?

【问题讨论】:

    标签: ios objective-c pdf uiwebview


    【解决方案1】:

    沙盒路径错误

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

    请使用您的沙盒路径而不是捆绑路径。

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

    【讨论】:

    • 你能解释一下吗?我在哪里可以使用NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    • 试试这个。NSString *resourceDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    • 替换你的NSString *resourceDocPath = [[NSString alloc] initWithString:[ [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents" ]];
    • 你是对的!它有效,谢谢!您能告诉我如何将 PDF 保存在 iPhone 中的 Documents 或 iBooks 应用程序中吗?所以用户可以在下载后随时阅读 PDF 而无需打开我的应用程序?
    • 谢谢你,我使用了 UIDocumentInteractionController,我让用户选择他想要放置 PDF 的应用程序 :)
    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多