ios11 UIDocumentInteractionController presentPreviewAnimated freezes the app

1.1k Views Asked by At

I've an issue with my iPad app on iOS11 when I want to show a preview of PDF files with this code :

self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:urlPathToDocument]];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentPreviewAnimated:YES];

This app freezes and I have to kill the app, she doesn't crash, and no error is shown.

On iOS10, no problem, the preview of the PDF file is shown perfectly without freeze the app.

I've tried to put this code in a dispatch_async bloc, but no change.

Could you help me ? Thank you.

2

There are 2 best solutions below

0
On BEST ANSWER

It seems to be a bug on my iPad, after 2 reboots, the problem seems to be resolved

0
On

Adding my own answer because I was running into a similar issue and this question came up in my search for solutions. The issue I was having is that the pdf would open in simulator fine but when I tried to open on a device it would just show a blank pdf page with the title of the pdf. I got an error saying something like "Couldn't issue file extension for path."

However, this is what I found:

Generating my url directly from the bundle would work for simulator but not devices

// This was not working on device, but did work with simulator  
let fileURL = Bundle.main.url(forResource: "SomePDF", withExtension: "pdf")!
self.docVC = UIDocumentInteractionController(url: fileURL)
self.docVC.delegate = self
self.docVC.presentPreview(animated: true)

So just to see what would happen I took that same url, converted it to data and saved to a temporary directory with a new url. This ended up working:

// Now it opens correctly on both simulator and device 
let url = Bundle.main.url(forResource: "SomePDF", withExtension: "pdf")!
let pdfData = try! Data(contentsOf: url)
let temp = NSTemporaryDirectory()
let fileURL = URL(fileURLWithPath: temp).appendingPathComponent("SomePDF.pdf")
try! pdfData.write(to: fileURL)
self.docVC = UIDocumentInteractionController(url: fileURL)
self.docVC.delegate = self
self.docVC.presentPreview(animated: true)

Hopefully this helps anyone else running into this issue. Was using iPhone X iOS 11.2.1