1

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?

gdm
  • 7,647
  • 3
  • 41
  • 71
  • Did you ever figure this out? I just ran into this with iOS8. – RyanJM Sep 25 '14 at 23:07
  • If I well remember, the file was not there, therefore there was nothing to show. Let me check the code.. – gdm Sep 27 '14 at 06:54
  • Sorry, I should have followed up with this. The issue I had was that I didn't save the file properly. Once the file was saved properly it all worked. – RyanJM Sep 29 '14 at 20:42

1 Answers1

3

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];
}
Aku
  • 844
  • 9
  • 16