iOS 6-可以从URL保存.pdf,可以在WebView中显示,不能将其移动到iBooks



这就是我要做的:

  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"实际上在Documents文件夹中,这很清楚,因为WebView已正确填充。

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

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

最新更新