9

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 !

Sam
  • 283
  • 3
  • 12
  • I did the same thing while ago in Swift, however it was UIWebView not WKWebView but assume it should be the same. – Siavash Alp Sep 04 '16 at 22:28
  • Hi and thank you for your answer. How did you manage this on UIWebView ? – Sam Sep 05 '16 at 18:57
  • hey, so what I did was I added `UILongPressGestureRecognizer` to the `UIWebView` and also disable the `actionSheet` on that, then on LongPress my action was getting triggered and I was presenting a new view on top of the WebView on each LongPress. – Siavash Alp Sep 05 '16 at 22:22
  • 3
    Thanks again for your answer. I have two issue with this method : 1 : how to disable the original long press action ? 2 : how to know the image file link where my finger is pressing on ? – Sam Sep 07 '16 at 09:50
  • Any update on that ? – Randy Dec 07 '16 at 15:24
  • Hi all, after all this time I still don't know how to do it... I have a function that work if I longtap on the screen, BUT only if the default menu is not trigerred. So I'm still wandering how to disable the default longpress menu action. – Sam Sep 30 '17 at 19:47

3 Answers3

1

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

dengST30
  • 3,643
  • 24
  • 25
1

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) ?? " _ | _ ")
            }
        }
   }
black_pearl
  • 2,549
  • 1
  • 23
  • 36
0

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() {
}
adriank
  • 143
  • 11