I've heard varying things about whether or not creating a Subclass of UIWebView is allowable. Can someone link me to any documentation that clarifies this one way or another?
Asked
Active
Viewed 3,207 times
2 Answers
10
There are mixed messages coming from Apple on this.
The docs do say not to subclass as BoltClock noted. However, one of the presentations from WWDC 2011, Rich Text Editing in Safari on iOS, suggests subclassing. It appears to be the only way to add custom UIMenuItems.
From the slides:
// For your UIWebView subclass:
- (void)bold:(id)sender {
[self stringByEvaluatingJavaScript:@”document.execCommand(‘Bold’)];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(bold:))
return YES;
return [super canPerformAction:action withSender:sender];
}
I need functionality other than Copy and Paste in my app so I'll be subclassing.

Justin Milo
- 575
- 4
- 15
-
My own interpretation is that Apple does not want us overriding the gnarly complicated guts of UIWebView such as HTML parsing, page layout rendering, and CSS processing. That is certainly understandable. On the other hand, overriding `canPerformAction:withSender:` to control the selection popup menu ([UIResponderStandardEditActions](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponderStandardEditActions_Protocol/index.html) and such) seems innocuous and appropriate. Ex: [this Question](http://stackoverflow.com/q/5995210/642706), disabling Select/Cut/Copy commands. – Basil Bourque Nov 14 '14 at 08:10
6
Under Subclassing Notes of the UIWebView
reference, all it says is:
Subclassing Notes
The UIWebView class should not be subclassed.
It doesn't say why. I would guess that it's to maintain the integrity of the underlying WebKit control or something, I dunno.
-
1I can make guesses myself as to why they might, I know that they have never rejected an app that I've submitted that subclassed UIWebView, and I feel better doing that then some of the hacks that I've seen people do to avoid doing so. (For instance, in my case I'm interested in the UIScrollView delegate messages, and the only other way to get these is by delving into the subviews of the UIWebView, which is even more dangerous as that's private land). Any thoughts on whether or not this should be adhered? – BadPirate Feb 04 '11 at 18:42