UIWebView Bug: - [UIWebView cut:]:无法识别的选择器发送到实例

pau*_*lvs 17 iphone uiwebview ios ios7

UIWebView,如果包含文本的输入元素具有焦点,并且按下按钮导致输入失去焦点,则随后双击输入以重新获得焦点并从弹出栏中选择剪切(或复制或粘贴)出现导致UIWebView崩溃的错误:

-[UIWebView cut:]: unrecognized selector sent to instance 0x10900ca60
Run Code Online (Sandbox Code Playgroud)

演示项目:https://github.com/guarani/WebViewDoubleTapTestTests.git

我认为这肯定是一个UIWebView错误,任何想法?

为了完整起见,这是我的网页视图的内容,

<html>
    <head>
    </head>
    <body>
        <br><br>
        <input type="text">
        <input type="button">
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

提交了Apple的Bug报告:15894403

更新2014/10/15:iOS 8.0.2中仍然存在错误(12A405)

Leo*_*ica 14

这是一个Apple bug.问题是cut:操作在响应器链中发送错误,最终被发送到UIWebView实例而不是内部UIWebDocumentView,这实现了方法.

在Apple修复bug之前,让我们对Objective C运行时有一些乐趣.

在这里,我通过将它们转发到正确的内部实例来UIWebView支持所有UIResponderStandardEditActions方法的子类.

@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);

                break;
            }
        }
    }

    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];
}

@end
Run Code Online (Sandbox Code Playgroud)

在故事板中使用此子类.

玩得开心.


use*_*516 5

如果您不介意没有剪切/粘贴/等标注。在这种情况下,当UIWebview错误地成为第一响应者时,则也可以使用此类别进行修复。这不禁止剪切/粘贴/等。当UIWebDocumentView(正确)成为第一响应者时。

@implementation UIWebView (NoWrongPerformWebview)

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

@end
Run Code Online (Sandbox Code Playgroud)

//兼容Swift 4的版本

import UIKit

extension UIWebView {

    override open func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // Should be respond to a certain Selector ??
        return responds(to: action)
    }
}
Run Code Online (Sandbox Code Playgroud)