25

I want to disable long-touch from the application. I have no control on the HTML that I am loading on my WebView.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71

4 Answers4

53

In webViewDidFinishLoad delegate I run a javascript on the loaded html page that disable the long touch.

[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'"];
Alex Terente
  • 12,006
  • 5
  • 51
  • 71
  • I have added the same line.. But it's not working. Don't know what is going wrong.. :( In most of the links I found the same solution as above.. – iGatiTech Oct 05 '15 at 12:49
  • - (void)webViewDidFinishLoad:(UIWebView*)theWebView { [theWebView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'"]; } But still not working for me – Achal Gandhi Feb 08 '17 at 12:44
  • @Denny this answer is from 2015 I might that webkit have changed a bit since then. The idea should be the same, inject javascript in to the web view that will disable the long touch. – Alex Terente Oct 01 '18 at 13:13
  • You can also add : `[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];` for Objective-C or `webView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitUserSelect='none';")` for Swift – Bejil Jun 28 '19 at 15:25
9

The UIWebView is no longer supported. So, you should implement WKWebView and there is a property called allowsLinkPreview with that you can enable or disable the long touch previews.

 webView.allowsLinkPreview = false
Mehmet Baykar
  • 367
  • 3
  • 11
1

You could try to override the following methods from the view controller :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

this way you can replace the usual touches management with your own implementation.

mbritto
  • 898
  • 12
  • 25
-6

Open your view in Interface Builder, click on the web view and then un-check the "User Interaction Enabled" checkbox in the attributes inspector.

If you want to do it in code, set the web view's userInteractionEnabled property to NO.

Cœur
  • 37,241
  • 25
  • 195
  • 267
winsmith
  • 20,791
  • 9
  • 39
  • 49