I want to disable long-touch from the application. I have no control on the HTML that I am loading on my WebView.
Asked
Active
Viewed 1.5k times
25
-
1https://stackoverflow.com/questions/5995210/disabling-user-selection-in-uiwebview – Yifan Jan 23 '19 at 06:10
4 Answers
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
.