paulvs paulvs - 1 year ago 236
iOS Question

UIWebView Bug: -[UIWebView cut:]: unrecognized selector sent to instance

In the

, if an input element containing text has focus, and a button is pressed that causes the input to lose focus, then subsequently double-tapping on the input to regain focus and selecting Cut (or Copy or Paste) from the popup bar that appears causes the
to crash with the error:

-[UIWebView cut:]: unrecognized selector sent to instance 0x10900ca60

Demo project:

I think this must be a
bug, any ideas?

For completeness, here are the contents of my web view,

<input type="text">
<input type="button">

Filed a Bug Report at Apple: 15894403

Update 2014/10/15: Bug still present in iOS 8.0.2 (12A405)

Answer Source

This is an Apple bug. The problem is the cut: action is sent incorrectly in the responder chain, and ends up being sent to the UIWebView instance instead of the internal UIWebDocumentView, which implements the method.

Until Apple fixes the bug, let's have some fun with the Objective C runtime.

Here, I subclass UIWebView with the purpose of supporting all UIResponderStandardEditActions methods, by forwarding them to the correct internal instance.

@import ObjectiveC;    

@interface CutCopyPasteFixedWebView : UIWebView @end

@implementation CutCopyPasteFixedWebView

- (UIView*)_internalView
    UIView* internalView = objc_getAssociatedObject(self, "__internal_view_key");

    if(internalView == nil && self.subviews.count > 0)
        for (UIView* view in self.scrollView.subviews) {
            if([view.class.description hasPrefix:@"UIWeb"])
                internalView = view;

                objc_setAssociatedObject(self, "__internal_view_key", view, OBJC_ASSOCIATION_ASSIGN);


    return internalView;

void webView_implement_UIResponderStandardEditActions(id self, SEL selector, id param)
    void (*method)(id, SEL, id) = (void(*)(id, SEL, id))[[self _internalView] methodForSelector:selector];

    //Call internal implementation.
    method([self _internalView], selector, param);

- (void)_prepareForNoCrashes
    NSArray* selectors = @[@"cut:", @"copy:", @"paste:", @"select:", @"selectAll:", @"delete:", @"makeTextWritingDirectionLeftToRight:", @"makeTextWritingDirectionRightToLeft:", @"toggleBoldface:", @"toggleItalics:", @"toggleUnderline:", @"increaseSize:", @"decreaseSize:"];

    for (NSString* selName in selectors)
        SEL selector = NSSelectorFromString(selName);

        //This is safe, the method will fail if there is already an implementation.
        class_addMethod(self.class, selector, (IMP)webView_implement_UIResponderStandardEditActions, "");

- (void)awakeFromNib
    [self _prepareForNoCrashes];

    [super awakeFromNib];


Use this subclass in your storyboard.

Have fun.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download