Disabling user selection in UIWebView
Here are a few ways to disable selection:
Add the following to your mobile web documents
<style type="text/css">* { -webkit-touch-callout: none; -webkit-user-select: none; /* Disable selection/copy in UIWebView */}</style>
Programmatically load the following Javascript code:
NSString * jsCallBack = @"window.getSelection().removeAllRanges();"; [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
Disable the Copy / Paste user menu:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:) || action == @selector(paste:)|| action == @selector(cut:)) { return _copyCutAndPasteEnabled; } return [super canPerformAction:action withSender:sender];}
I can confirm that the following code works in iOS 5.0 - 8.0.
- (void)webViewDidFinishLoad:(UIWebView *)webView { // Disable user selection [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"]; // Disable callout [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];}
Also works for iOS 9 and later. Here's the swift code:
func webViewDidFinishLoad(webView: UIWebView) { // Disable user selection webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none'")! // Disable callout webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitTouchCallout='none'")!}
I am using this technique in a web app for Android / iPhone (packaged with Trigger.IO) and found it would only work with the chaining syntax for the :not() pseudo-class, :
*:not(input):not(textarea) {-webkit-user-select: none; /* disable selection/Copy of UIWebView */ -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */}