简体   繁体   中英

WKWebView : How to change Long press action or disable it?

Hi everyone (why my greeting message is always deleted??? !!! )

Even I read many topics about it, I didn't find a proper answer about my issue.

I would like to know if it's possible to disable a long press event on a wkwebview window or detect when the user use a long press event to do whatever I want?

Actually I would like to copy an image link from a web site into a variable by using long press event and I don't want the popup to be called at all.

I'm using Swift

Thank you for your help !

you can use this to disable the pop up view, while the highlighting is still there

func swizzle() {

    guard let cls = NSClassFromString("UICalloutBar") else { return }
    let originalSelector = NSSelectorFromString("appear")
    let swizzledSelector = #selector(UIView.appearHijack)
    let originMethod = class_getInstanceMethod(cls, originalSelector)
    let swizzleMethod = class_getInstanceMethod(UIView.self, swizzledSelector)
    if let swizzledMethod = swizzleMethod, let originalMethod = originMethod{
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
    
    
}
        



extension UIView{
    @objc func appearHijack(){
        
    }
}

swizzle() should be call once only.


How I know UICalloutBar ?

Just use break point , also bt is an option.

888

just hijack the relevant,

func swizzle(){
    guard let cls = NSClassFromString("UITextSelectionView") else { return }
    let originalSelector = NSSelectorFromString("updateSelectionRects")
    let swizzledSelector = #selector(UIView.updateSelectionRectsHijack)
    let originMethod = class_getInstanceMethod(cls, originalSelector)
    let swizzleMethod = class_getInstanceMethod(UIView.self, swizzledSelector)
    if let swizzledMethod = swizzleMethod, let originalMethod = originMethod{
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}

extension UIView{
    @objc func updateSelectionRectsHijack(){ }
}

swizzle() should be call just once.


What is UITextSelectionView ?

Check View Hierarchy ,

999


What is method updateSelectionRects ?

via runtime ,

import ObjectiveC
    {
        var count: UInt32 = 0
        guard let methodArr = class_copyMethodList(NSClassFromString("UITextSelectionView"), &count) else { return }
        
        let cnt = Int(count)
        for i in 0..<cnt{
            let method = methodArr[i]
            let name = method_getName(method)
            if let type = method_getTypeEncoding(method){
                print(name, String(utf8String: type) ?? " _ | _ ")
            }
        }
   }

For iOS 16:

@available(iOS 16.0, *)
func swizzleiOS16() {
    guard let cls = NSClassFromString("UIEditMenuInteraction") else { return }
    let originalSelector = NSSelectorFromString("presentEditMenuWithConfiguration:")
    let swizzledSelector = #selector(UIEditMenuInteraction.presentEditMenuWithConfigurationHijack)
    let originMethod = class_getInstanceMethod(cls, originalSelector)
    let swizzleMethod = class_getInstanceMethod(UIEditMenuInteraction.self, swizzledSelector)
    if let swizzledMethod = swizzleMethod, let originalMethod = originMethod{
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}

@available(iOS 16.0, *)
extension UIEditMenuInteraction{
    @objc func presentEditMenuWithConfigurationHijack() {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM