I want to disable long-touch from the application. I have no control on the HTML that I am loading on my WebView.
-
1stackoverflow.com/questions/5995210/…– YifanJan 23, 2019 at 6:10
4 Answers
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'"];
-
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.. Oct 5, 2015 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 Feb 8, 2017 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. Oct 1, 2018 at 13:13
-
You can also add :
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
for Objective-C orwebView.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitUserSelect='none';")
for Swift– BejilJun 28, 2019 at 15:25
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
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.
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
.