In my application, I use UIWebView to display different types of files like, html, pdf, doc etc.
I am customizing the contextual menu on html content, by disabling callout and using gesture recognizer. It works fine for html content.
However when displaying pdf content web view uses different classes from corepdf framework which is private framework. So none of the technique which worked on html content works with pdf content.
UIWebView uses UIPDFPageView and related classes to display contextual menu on long pressing on hyperlink.
Is there any method to override the display of contextual menu without having to deal with private framework?
With clever tricks, you can override the underlying
UIWebDocumentView
's-canPerformAction:withSender:
method to only pass what you wish to display. This should work for PDF as well, as the internal classes are deeper in the view hierarchy and you should be fine with the UIWebDocumentView.On iOS5, 6 and 7 seed 1, the
UIWebDocumentView
object is a subview of the scroll view of theUIWebView
. What I do is find the subview of the scroll view that has the@"UIWeb"
prefix in its class name, and use the Objective C runtime to dynamically subclass it to my own subclass (I create it at runtime - like what the system does for key-value observing) and replace the implementation with my own. You can even call the previous implementation by calling thesuper
.This is hacky, but it seems to be safe. Apple does not seem to have introduced the off-process UIWebView (using XPC), so it should be safe with iOS7 as well.
As always the disclaimer,
this API is not documented and is subject to change at any given moment, and your app may stop functioning at any time®
.