How to disable Copy and Paste in UIWebView

前端 未结 6 910
再見小時候
再見小時候 2020-12-09 13:17

When a user long-press inside UIWebView, there is a Copy & Paste popup. Is it possible to disable the system from popup the Copy & Paste function, but still allow th

6条回答
  •  隐瞒了意图╮
    2020-12-09 13:47

    For anyone who can operate at the HTML level, the JavaScript solution is the way to go (extract the JavaScript part from here [1]).

    For dev that can't modify the HTML pages, [1] solution will work for 99% of clients and is really clean and safe.

    However for the cases where the popup that appears when you long press a link or the copy and paste or the magnifying glass etc just should never, then here it come my working solution. (the cases where the JavaScript injection fails are those where the pages takes a bit to load and the user long presses a link in the meantime).

    To solve the problem, just paste this protocol implementation almost anywhere in your code (don't be lazy...make a new category file). Please be aware that this solution is dangerous, at least theoretically, in real life (i.e. as of iOS 6.0.2) it's not dangerous. Please know what categories are and what this solution involves.

    @implementation UIScrollView (CustomGestureCollisionHandling)
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
        for(UIView *aView in gestureRecognizer.view.subviews)
        {
            for (UIGestureRecognizer *gestRec in aView.gestureRecognizers) 
            {
                if (!gestRec.enabled) 
                {
                    continue;
                }
    
                if ([[NSString stringWithFormat:@"%@",[gestRec class]] isEqualToString:@"UITapAndAHalfRecognizer"]) 
                {
                    gestRec.enabled = NO;
                }
            }
        }
    
        if ([otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) 
        {
            otherGestureRecognizer.enabled = NO;
        }
    
        return NO;
    
    }
    
    @end
    

    [1] https://stackoverflow.com/a/5548362/428143

提交回复
热议问题