Disable copy and paste in UIWebView

Deadly 提交于 2019-12-03 08:54:50
erno
-webkit-user-select: none; /* Disable selection/Copy of UIWebView */

will also disable form on Mobile Safari.

sergio

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.

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