【问题标题】:Saving a PDF from UIWebView and opening it with UIDocumentInteractionController to iBooks从 UIWebView 保存 PDF 并使用 UIDocumentInteractionController 打开它到 iBooks
【发布时间】:2015-01-06 13:07:21
【问题描述】:

好吧,我有点烧脑了。

我只是想保存一个加载到 UIWebView 中的 PDF 文件。

这就是我所拥有的:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir   == NO)
{
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *filePath = [cachePath stringByAppendingPathComponent:@"MissaoPilotoPortuguese.pdf"];
NSData *pdfFile = [NSData dataWithContentsOfURL:webView.request.URL];
[pdfFile writeToFile:filePath atomically:YES];

但由于某种原因,当我调用它打开 PDF 文件时,应用程序崩溃说 url 路径为 nil:

  NSString *path = [[NSBundle mainBundle] pathForResource:@"MissaoPilotoPortuguese" ofType:@"pdf"];
NSURL *urls = [NSURL fileURLWithPath:path];


self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;

[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

关于我做错了什么有什么想法吗?

谢谢

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    在要保存的代码中,通过请求缓存目录然后附加文件名来构造 URL。

    然后,当您尝试在第二个 sn-p 中打开它时,您只需询问主包的目录 - 这与您上面的目录不同。

    你的第二个 sn-p 应该看起来更像:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"MissaoPilotoPortuguese.pdf"];
    NSURL *urls = [NSURL fileURLWithPath:filePath];
    ...
    

    或者,如果这一切都发生在同一个方法中,只需重用您之前构造它时的filePath

    【讨论】:

    • 完美。谢谢你的解释。
    猜你喜欢
    • 2011-07-16
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    相关资源
    最近更新 更多