UIWebView contextual menu while displaying PDF con

2019-06-04 20:05发布

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?

1条回答
叼着烟拽天下
2楼-- · 2019-06-04 20:08

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 the UIWebView. 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 the super.

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®.

查看更多
登录 后发表回答