【问题标题】:download PDF document from application to local storage and then open with iBook将 PDF 文档从应用程序下载到本地存储,然后使用 iBook 打开
【发布时间】:2013-07-02 12:06:03
【问题描述】:

我的应用上有一个名为“下载说明 pdf”的 html 链接

当我单击此按钮时,我想将 pdf(存储在应用程序中)下载到本地 iPad / iPhone,然后用 iBook 打开它。

很多类似的问题都提到了 WebView 并使用了 ViewController,但我已经完成了大部分应用程序,当我使用 webview 时它会打开一个空白页面并且应用程序无法运行。

这是我的第一个 iOS 应用,之前我没有使用过 Objective C,所以在使用 ViewController 时会感到困惑。

这是我目前的代码:

<a href="howtouse.pdf">download instructions pdf</a>

这会打开 pdf,但会以全屏形式显示,从而删除任何导航。因此,如果用户想要访问应用程序的其他部分,他们必须重新打开对用户不友好的应用程序!

我使用的是 xcode 4.6.3 iPad/iPhone 模拟器 6.1

我被困了几个小时,任何帮助都会很棒,谢谢!

【问题讨论】:

  • 您坚持以上哪一部分?什么有效,什么无效?
  • 目前我只是使用 下载说明 pdf 在应用程序中打开 PDF 文件,但这删除了应用程序上的所有导航。因此,如果用户想继续使用该应用,他们需要重新打开它。

标签: ios objective-c pdf download


【解决方案1】:

对于下载部分,请看一些NSURLConnection 示例

将 PDF 下载到 NSData 后,将其写入我可以通过以下方式找到的 Documents 路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"yourFilename"];

要打开它,请尝试 UIDocumentInteractionController this way

【讨论】:

    【解决方案2】:

    我创建了一个使用 UIDocumentInteractionController 的示例。我上传到 Github,你可以在这里下载:

    https://github.com/cjimenezpacho/openpdfibooks

    基本上是 IBAction 中的这段代码和所需的委托方法(非常类似于链接 sp4rkbr):

    NSURL *url = [[NSBundle mainBundle]
    URLForResource: @"iPhoneintro" withExtension:@"pdf"];
    self.docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    self.docController.delegate = self;
    [self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    

    Apple 文档链接:

    类: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多