Disable copy and paste in UIWebView

本小妞迷上赌 提交于 2019-12-04 14:00:16

问题


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


回答1:


-webkit-user-select: none; /* Disable selection/Copy of UIWebView */

will also disable form on Mobile Safari.




回答2:


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.




回答3:


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 */
 }


来源:https://stackoverflow.com/questions/11290613/disable-copy-and-paste-in-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!