1

这就是我想要做的:

  1. 从外部 URL 获取 .pdf
  2. 保存到我的本地磁盘
  3. 在 WebView 中显示它
  4. 允许用户将 .pdf 移动到另一个可以阅读 .pdf 的应用

从 1 到 3 的所有内容都可以正常工作。但是没有任何东西被移动/共享到/与其他应用程序。我不明白我做错了什么。这就是我正在做的事情。

我如何将 pdf 保存在 Documents 文件夹 (viewDidLoad) 中:

// to save the pdf into local file system (tempString is the pdf url)
NSData *pdfData = [[NSData alloc] 
    initWithContentsOfURL:[NSURL URLWithString:tempString]];
NSString *resourceToPath = [[NSString alloc] 
    initWithString:[[[[NSBundle mainBundle] resourcePath]
    stringByDeletingLastPathComponent] 
    stringByAppendingPathComponent:@"Documents"]];
NSString *filePAth = [resourceToPath stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePAth atomically:YES];

// to populate the WebView
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[my_web_view setUserInteractionEnabled:YES];
//[editoriale_view setDelegate:self];
[my_web_view loadRequest:requestObj];

在我的 viewDidLoad() 函数中,我创建了一个按钮,允许用户打开可以读取 .pdf 文件的应用程序列表:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self 
    action:@selector(show_Button)];

这是我的 show_Button 函数:

-(void)show_Button {
    NSString *resourceToPath = [[NSString alloc] 
        initWithString:[[[[NSBundle mainBundle] resourcePath] 
        stringByDeletingLastPathComponent] 
        stringByAppendingPathComponent:@"Documents"]];
        NSString *filePAth = [resourceToPath 
        stringByAppendingPathComponent:@"myPDF.pdf"];

    NSLog(@"filePath = %@", filePAth);
    NSURL *url2 = [NSURL fileURLWithPath:filePAth];    
    NSLog(@"url2 = %@", url2);

    UIDocumentInteractionController *docContr = [UIDocumentInteractionController 
        interactionControllerWithURL:url2];
    [docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
 }

当我在我的设备上尝试这个时,一切正常,直到我点击列表中的一个图标(即 iBooks 之一)。然后应用程序关闭(它不会崩溃,它只是关闭)。

这是我在 show_Button 函数中放置的两个日志的控制台打印内容:

1. filePath = /Users/[MY_USER]/Library/Application Support/iPhone
    Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
2. url2 = file://localhost/Users/[MY_USER]/Library/Application%20Support/
    iPhone%20Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf

有人想让我明白我做错了什么吗?我正在使用 Xcode 4.6。我使用第三方软件浏览了我的 iPhone 应用程序文件系统,文件“MyPDF.pdf”实际上位于“文档”文件夹中,这很清楚,因为 WebView 已正确填充。

4

2 回答 2

0

显示文档控制器时更改CGRectZero为 self.view.bounds。

于 2013-02-08T16:01:53.980 回答
0

解决了。我没有在 .h 文件中实现 UIDocumentenInteractionController 委托。现在我有了,一切正常。感谢@trojanfoe 提供有用的提示。

于 2013-02-20T10:37:06.107 回答