8
votes

I'm trying to display a pdf on iOS devices using the UIDocumentInteractionController presentPreviewAnimated method, but it keeps displaying a blank document. I think it might have to do with the character encoding, but I'm not sure. If I use a UIWebView, I can get the pdf to display, just not with the document interaction controller.

// UPDATE 9/18/14 This is now working with the GM release of Xcode 6.

// UPDATE 8/22/14

Oddly enough, from the DocumentInteractionController, if I tap on the "Open In" icon in the top right corner and choose something like iBooks, the pdf displays correctly. It seems as though it's just the preview that doesn't want to display it on the screen.

Here's my code (in Swift):

// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIDocumentInteractionController(data: NSData) {

    let fileName = NSTemporaryDirectory().stringByAppendingPathComponent("myFile.pdf")
    let url: NSURL! = NSURL(fileURLWithPath: fileName)

    // this does not seem to make a difference
    // let pdfString = NSString(data: data, encoding: NSISOLatin1StringEncoding)
    // pdfString.writeToURL(url!, atomically: true, encoding: NSISOLatin1StringEncoding, error: nil)

    data.writeToURL(url, atomically: true)
    if url != nil {
        let docController = UIDocumentInteractionController(URL: url)
        docController.UTI = "com.adobe.pdf"
        docController.delegate = self
        docController.presentPreviewAnimated(true)
    }
}

This code does display the pdf correctly:

// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIWebView(data: NSData) {

    let rect = UIScreen.mainScreen().bounds
    let screenSize = rect.size

    let webView = UIWebView(frame: CGRectMake(0,0,screenSize.width,screenSize.height))
    webView.autoresizesSubviews = true
    webView.autoresizingMask = (UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth)

    webView.loadData(data, MIMETYype: "application/pdf", textEncodingName: "ISO-8859-1", baseUrl: nil)

    self.view.addSubview(webView)
}

Is there any reason the first function should not be working? It doesn't error out, just displays a blank page.

2
I have the exact same problem. My code is similar to yours. You are not alone ! I will keep you updated if I find something :)Emilie
Thanks @Emilie, this is driving me crazy!Chris Conway
Could you provide the URL of the PDF? Because, I cannot reproduce the problem with my PDF files.(with Objective-C/iOS7)rintaro
I have the same Problem here. Only with PDF, all other files work fine. I guess thats just a bug, hopefully get fixed soon.derdida
On iOS7 works OK, I have the same issue on iOS8, and not only with pdf. Quick look is also causing memory issues for some large movie types (ex mkv). As a workaround I replaced presentOpenInMenuFromRect with presentOptionsMenuFromRect, this way Quick look option is not available anymore.Calin Chitu

2 Answers

1
votes

I'm not using Swift, but I had basically the same problem with straight up Objective-C. Before iOS8, my UIDocumentInteractionController displayed pretty much every file type i threw at it including PDF. But in iOS8, the PDF files would no longer display for me.

I WAS creating it this way:

[[[UIDocumentInteractionController alloc] init] autorelease]

I changed the call to create it like this:

[UIDocumentInteractionController interactionControllerWithURL:myUrl]

And now my PDF files display again (and the others appear to be ok too still).

0
votes

This is working with the GM release of Xcode. Guess it was just a bug.