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 goto new pages?
-
1possible duplicate of [How to disable long touch in UIWebView on iPad?](http://stackoverflow.com/questions/4314193/how-to-disable-long-touch-in-uiwebview-on-ipad) – Alex Terente Jul 13 '11 at 08:51
-
possible duplicate of [Disabling user selection in UIWebView](http://stackoverflow.com/questions/5995210/disabling-user-selection-in-uiwebview) – Basil Bourque Nov 15 '14 at 06:10
7 Answers
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';"];
}

- 697
- 1
- 9
- 15
-
I think this is the best solution. The same can also be done with (inline) CSS: * { -webkit-touch-callout: none; -webkit-user-select: none; } – jn-se Jul 26 '17 at 13:34
Try this
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'"];

- 6,464
- 3
- 47
- 90
-
This solution is not working for me. can any one help me to stop display Copy & Paste popup on UiWebView – Achal Gandhi Feb 08 '17 at 12:41
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
- 1

- 340
- 3
- 12
-
this was a life saver for me to disable the really annoying double tap and hold function which brought up a magnifying glass - thanks!! – Alex Fox Jul 28 '15 at 20:23
-
1Pre-iOS 9, this was a great solution! However, with iOS 9, all of a sudden in my App, the user could not dismiss UIAlertView and UIAlertController's. It turns out, on iOS 9+ the CustomGestureCollisionHandling category is interfering with the gesture recognizers used by UIAlertView and UIAlertController. According to Apple "Attaching categories to UIKit classes that overwrite the default implementation(s) of methods, or implements delegate methods for objects used by that UIKit class is not supported and prone to break from release to release." Just a heads-up. – PaulPerry Nov 13 '15 at 18:42
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

- 1
- 1

- 3,008
- 1
- 25
- 35
Tested in iOS 5,6,7:
Hide the whole context menu with:
[[UIMenuController sharedMenuController] setMenuVisible:NO];
on event UIMenuControllerWillShowMenuNotification
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.

- 10,221
- 5
- 83
- 96
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

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

- 143
- 1
- 8