Reputation: 7920
I can successfully open png files, but when I try to open pdf, the preview says "Portable Document Format" without showing the content.
self.docInteractionController.UTI = @"com.adobe.pdf";
if ([operation isEqualToString:@"open"])
if (![self.docInteractionController presentPreviewAnimated:YES])
NSLog(@"Failed to open document in Document Dir");
Any hint?
Upvotes: 1
Views: 2863
Reputation: 864
For anyone else who ends up here... As pointed out by RyanJM, this seems to suggest that the file isn't actually saved as expected or where the URL suggests it is. Another possibility is that you aren't opening a local URL. Whilst UIDocumentInteractionController can take a URL, it must begin with file://
You can download locally and then open with something like this:
// Build URLS
NSURL* url = [[NSURL alloc] initWithString:@"http://MYURLGOESHERE"];
NSURL* documentsUrl = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
NSURL* destinationUrl = [documentsUrl URLByAppendingPathComponent:@"temp.pdf"];
// Actually download
NSError* err = nil;
NSData* fileData = [[NSData alloc] initWithContentsOfURL:url options:NSDataReadingUncached error:&err];
if (!err && fileData && fileData.length && [fileData writeToURL:destinationUrl atomically:true])
{
// Downloaded and saved, now present the document controller (store variable, or risk garbage collection!)
UIDocumentInteractionController* document = [UIDocumentInteractionController interactionControllerWithURL:destinationUrl];
document.delegate = self;
[document presentPreviewAnimated:YES];
}
Upvotes: 3