Intercepting PDF Link Clicks using Quicklook Frame

2019-05-06 20:13发布

问题:

Is PDF Link handling available in Quicklook?

I created a prototype, and the QLPreviewControllerDelegate method -(BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id)item never gets called.

When I use a .docx file with links in it, the delegate method does get fired.

Also, when I open up the PDF using safari, or a UIWebView, link handling works fine.

I tested with a few different PDFs, and link interception never worked.

I made sure that My interface implements the following protocols: QLPreviewControllerDataSource, QLPreviewControllerDelegate

I also made sure that the class that has the shouldOpenUrl method is assigned as the delegate to my QlPreviewController instance.

I am guessing the issue is not with my code, since the .docx link interception works fine.

Any thoughts on what I might be doing wrong?

Is PDF Link interception even supported by Quicklook?

I also noticed that when I open up a PDF in iOS mail, it uses Quicklook, and PDf links don't work there either.

回答1:

Answering my own question for those that land here looking for the same answer. Quicklook does NOT support link handling. You'll have to use CATiledLayer to render PDFs if you need to handle links in PDF documents.



回答2:

You can detect when a link is clicked in a document via the QLPreviewControllerDelegate

/*
 * @abstract Invoked by the preview controller before trying to open an URL tapped in the preview.
 * @result Returns NO to prevent the preview controller from calling -[UIApplication openURL:] on url.
 * @discussion If not implemented, defaults is YES.
 */
- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item;

Returning YES will open that document, if it's an external URL it will automatically launch Safari.

Note, you may want to show UIAlertView to ask the user if they want to exit the app to open Safari.