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

后端 未结 4 1744
时光取名叫无心
时光取名叫无心 2021-02-04 03:43

In the UIWebView, 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 th

4条回答
  •  鱼传尺愫
    2021-02-04 04:06

    If anyone is interested, here's the swift version of Leo Natans method :

    import Foundation
    import ObjectiveC
    
    var AssociatedObjectHandle: UInt8 = 0
    
    
    class CustomWebView: UIWebView {
        func _internalView() -> UIView? {
            var internalView:UIView? = objc_getAssociatedObject(self, "__internal_view_key") as? UIView
            if internalView == nil && self.subviews.count > 0 {
                for view: UIView in self.scrollView.subviews {
                    if view.self.description.hasPrefix("UIWeb") {
                        internalView = view
                        objc_setAssociatedObject(self, "__internal_view_key", view, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
                    }
                }
            }
            return internalView
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
            self._prepareForNoCrashes()
        }
    
        func _prepareForNoCrashes() {
            let selectors = ["cut:", "copy:", "paste:", "select:", "selectAll:", "delete:", "makeTextWritingDirectionLeftToRight:", "makeTextWritingDirectionRightToLeft:", "toggleBoldface:", "toggleItalics:", "toggleUnderline:", "increaseSize:", "decreaseSize:"]
            for selName: String in selectors {
                let selector = NSSelectorFromString(selName)
                //This is safe, the method will fail if there is already an implementation.
                let swizzledMethod:IMP = class_getInstanceMethod(CustomWebView.self, #selector(CustomWebView.webView_implement_UIResponderStandardEditActions))
                class_addMethod(CustomWebView.self, selector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
            }
        }
    
        func webView_implement_UIResponderStandardEditActions(this:AnyObject, selector:Selector, param:AnyObject)
        {
            let method = {(val1: UIView?, val2: Selector, val3: AnyObject) -> Void in
                self._internalView()?.methodForSelector(selector)
            }
    
            method(self._internalView(), selector, param);
        }
    
    }
    

提交回复
热议问题