获取 URL 到不在文档目录中的 PDF - UIDocumentInteractionController

Get URL to a PDF thats not in documents directory - UIDocumentInteractionController

我有一个每次都从保存的核心数据动态生成的 PDF,而不是作为 NSData self.pdfData 存储在文档目录中,呈现在 UIWebView 中。我不想使用 UIActivityController 来共享 PDF,而是使用 UIDocumentInteractionController 来获得全方位的共享选项。问题是这似乎只适用于文档目录中保存的 PDF。

NSURL *PDFUrl = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"]; 

我宁愿不将它保存到文档目录然后删除它,我希望有更优雅的东西。我尝试使用 self.webView.request.mainDocumentURL 直接从 PDF 的 UIWebview 获取 URL 但是这个 returns about:blank

NSURL *PDFUrl = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"]; <----- 

    if (PDFUrl) {
        DebugLog(@"Loading document");

        self.documentController = [UIDocumentInteractionController interactionControllerWithURL:PDFUrl];
        [self.documentController setDelegate:self];
        [self.documentController presentPreviewAnimated:YES];
    }

UIDocumentInteractionController 仅适用于本地文件系统上的文件。这意味着您必须先将 PDF 保存到文件,然后才能使用 UIDocumentInteractionController.

将 PDF 数据写入缓存文件夹 (NSCachesDirectory) 中的文件,获取其文件 URL,使用 UIDocumentInteractionController,完成后删除文件。

或使用 UIActivityViewController 并将 PDF 的 NSData 作为 activity 项传递。