开发者

How to disable Copy and Paste in UIWebView

开发者 https://www.devze.com 2023-03-20 03:40 出处:网络
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 the user to click on links an

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 the user to click on links and got开发者_如何学JAVAo new pages?


i hope this work for you, because is work for me

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
}


Try this

[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'"];


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


You can try injecting javascript into the webView. This code works on the iPhone too but only when the page is fully loaded. http://javascript.internet.com/page-details/disable-text-selection.html or http://solidlystated.com/scripting/proper-way-to-disable-text-selection-and-highlighting/

To get it to work properly when the page is only half loaded or still loading you'll proably have to use a setup similar to this one where you inject the disabling javascript just as it would start selecting. http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

UIWebView without Copy/Paste and selection rectangle when showing documents


Tested in iOS 5,6,7:

Hide the whole context menu with:

[[UIMenuController sharedMenuController] setMenuVisible:NO];

on event UIMenuControllerWillShowMenuNotification

Example

Note the selector is fired again after a delay. In the example, they use 0.15 secs. I used .001. That prevents the appearance better - or at least reduces the time the menu is visible / available.


from the reference of @issam answer, below is converted in Swift

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.body.style.webkitTouchCallout='none';")
        webView.evaluateJavaScript("document.body.style.webkitUserSelect='none';")
    }

its working in ios11


- (void)webViewDidFinishLoad:(UIWebView *)webView {
 [iWebView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';  document.body.style.KhtmlUserSelect='none'"];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号