用于远程pdf文件的UIDocumentInteractionController

问题描述:

是否有人知道如何使用UIDocumentInteractionController“在iBooks中打开”远程pdf文件,我似乎无法绕过此文件。我已经设法打开我的PDF在QLPreviewController,并获得OptionsMenu给我的选项打开iBooks,但我不会打开文件,如果它是远程...当我使用本地文件,它工作正常。用于远程pdf文件的UIDocumentInteractionController

如果这不可能有什么替代方案?

在此先感谢

虽然UIDocumentInteractionController有一个方便的方法interactionControllerForURL:,它需要的参数是一个文件URL。因此,您可以在您的应用程序中下载PDF并使用UIDocumentInteractionController对象打开它,也可以使用UIWebView对象打开远程PDF。将网址传递给网络视图,并打开状态良好。

+0

谢谢您的回答。关于如何在使用UIDocumentInteractionCntroller之前完成下载? – 2011-06-06 05:45:33

+0

我在我的应用程序中做同样的事情,我正在使用[AFNetworking](https://github.com/AFNetworking/AFNetworking)。 请参见“创建下载任务”部分。您可以获取文件的url路径并在UIDocumentInteractionController中使用它。 – 2013-12-10 18:09:56

广告提到你必须使用UIDOcumentInteractionController的文件url。首先下载文件。一个非常简单的方法是使用AFNetworking。这里是我如何使用AFNetworking下载文件:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)req navigationType:(UIWebViewNavigationType)navigationType { 
self.title = req.URL.description; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

NSURL *URL = req.URL; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 
    NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]]; 
    self.fileURLPath = [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]]; 
    return self.fileURLPath; 
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
    NSLog(@"File downloaded to: %@", filePath); 
}]; 
[downloadTask resume]; 


return YES; 

}

现在,你有fileURLPath你可以像这样创建UIDocumentInteractionController:

documentController = [UIDocumentInteractionController interactionControllerWithURL:self.fileURLPath]; 
+1

你确实意识到在这种情况下使用AFNetworking没有任何好处,并且你对99%的代码使用NSURLSession的东西.....而不是AFNetworking ... – TheCodingArt 2014-09-09 13:19:52