开发者

UIDocumentInteractionController for remote pdf files

Does anybody know how to use UIDocumentInteractionController to "Open in iBooks" remote pdf files, i can't seem to be able to get around this one. I have managed to open my pdf in QLPreviewController and get the OptionsMenu to give me the option to open in iBooks but i won't open the file if it is remote...when i开发者_如何学Go use local file it works fine.

If this is not possible what is the alternative?

Thanks in advance


Although UIDocumentInteractionController has a convenience method interactionControllerForURL:, it requires the argument to be a file URL. So you either download the PDF within your app and open it with the UIDocumentInteractionController object or you can use a UIWebView object to open remote PDFs. Pass the URL to the web view and they open just fine.


Ad mentioned you must use a file url for UIDOcumentInteractionController. First download the document. A really easy way to do this is with AFNetworking. Here is how I am using AFNetworking to download a file:

- (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;

}

Now that you have the fileURLPath you can create the UIDocumentInteractionController like so:

documentController = [UIDocumentInteractionController interactionControllerWithURL:self.fileURLPath];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜