3

Nearly, I had tried every thing to disable copy/paste in UIWebView but nothing worked with me.

I'm loading my UIWebView from a string (array of strings) as follows:

[webView loadHTMLString:
[NSString stringWithFormat:@"%@<p class=\"paragraph\"  style=\"float: right\"  >%@</p>",css,[[array objectAtIndex:0] valueForKey:@"content"]]   baseURL:nil ];

I had tried this:

-(void)webViewDidFinishLoad:(UIWebView *)webView1{
[webView1 stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"];
}

and this:

  NSString *css =
@"<head><style><body> *{-webkit-touch-callout: none; -webkit-user-select: none;}</style> </head>  </body> ";

but nothing worked with me especially for iOS 4.2

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mutawe
  • 6,464
  • 3
  • 47
  • 90

3 Answers3

7

It seems it is more complex that that... have a look at this thread on S.O which details all you have to do...

summary: you need to:

modify your CSS (like you do):

<style type="text/css">
* {
  -webkit-touch-callout: none;
  -webkit-user-select: none; /* Disable selection/Copy of UIWebView */
}
</style>

adding some javascript:

NSString * jsCallBack = @"window.getSelection().removeAllRanges();";    
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];

disable the copy/paste menu:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{
  BOOL superCanPerform = [super canPerformAction:action withSender:sender];
  if (superCanPerform) {
    if (action == @selector(copy:) ||
      action == @selector(paste:)||
      action == @selector(cut:)) 
    {
       return _copyCutAndPasteEnabled;
    }
  }
  return superCanPerform;
}

canPerformAction should be defined in your UIWebView; you have two options for that:

  1. defining a category for UIWebView (if it's ok to remove this behaviour from all of your UIWebViews);

  2. derive your own web view class from UIWebView and override that method in there.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • i have more than 7 web views, its very hard to rewrite them all – Mutawe Jul 02 '12 at 09:17
  • @Mutawe You should define a category for UIWebView and import it where you wanna to disable copy & paste operation. – asedra_le Jan 23 '13 at 01:49
  • 1
    @sergio Sorry, but I believe there is a little glitch in your canPerformAction code. You are returning if the action is enabled in an arbitrary manner, and maybe the web view does not support the paste for the current context. For that cases, the paste action will appear, but the actual action will crash. I think it's better to call super first, and if super allows the action, then check your flags/conditions. How this helps =D – Omer Aug 15 '13 at 13:51
  • @sergio I have similar problem here, but this solution does not fit. Please, check it out if you have a minute. Thanks http://stackoverflow.com/questions/18258522/uiwebview-disable-copy-cut-options-for-a-rich-text-editor – Omer Aug 15 '13 at 17:49
7
-webkit-user-select: none; /* Disable selection/Copy of UIWebView */

will also disable form on Mobile Safari.

cHao
  • 84,970
  • 20
  • 145
  • 172
erno
  • 86
  • 1
  • 2
  • 1
    This solution works for me on iOS7, but it causes other issues : once I add this code line, I can't type anything in my html input fields! – AmineG Oct 19 '13 at 14:34
0

Use this.

<style type="text/css">
*: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 */
}       
</style>
 If you want Disable only anchor button tag use this.

a {-webkit-user-select: none; /* disable selection/Copy of UIWebView */
   -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
 }
Narsingh Tomar
  • 487
  • 5
  • 15