Open Local PDF in Safari in iOS

2019-02-26 06:58发布

问题:

Let's say that myURL pointed to a PDF which was obtained by doing:

myURL = [[NSBundle mainBundle] URLForResource:@"my" withExtension:@"PDF"];

I can call [[UIApplication sharedApplication] canOpenURL:myURL] and see that it returns YES.

However, when I call [[UIApplication sharedApplication] openURL:myURL], nothing happens and it returns NO.

Is there a way to have this work?

Yes, I know I could use a UIWebView to have it open the PDF, but in this instance, I need the Safari App to open it, if possible. I am aware the natural consequence of this will be for it to exit my application. This behavior would be acceptable in this case.

Thank you.

回答1:

This is not currently possible.



回答2:

As far as I know it is not possible to hand a local PDF file off to MobileSafari.

But you can easily hand off a local PDF file to any locally installed application that acts as a PDF viewer. i.e., iBooks, GoodReader, etc. If this is sufficient, you should take a look at UIDocumentInteractionController. It will present a menu of apps, and let the user choose one to open the file in. Unfortunately, Safari itself does not register as a PDF viewer.

(If you did decide to present PDFs yourself, the QLPreviewController will display the file and give you the "send to other app" functionality.)