简体   繁体   English

在UIWebView中禁用复制和粘贴

[英]Disable copy and paste in UIWebView

Nearly, I had tried every thing to disable copy/paste in UIWebView but nothing worked with me. 差不多,我曾经尝试过在UIWebView禁用复制/粘贴的所有东西,但没有任何方法可以解决这个问题。

I'm loading my UIWebView from a string (array of strings) as follows: 我正在从字符串(字符串数组)加载我的UIWebView ,如下所示:

[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 但是没有任何关系,特别是对于iOS 4.2

It seems it is more complex that that... have a look at this thread on SO which details all you have to do... 似乎更复杂的是......看看SO上的这个帖子 ,详细说明你要做的一切......

summary: you need to: 总结:你需要:

modify your CSS (like you do): 修改你的CSS(就像你一样):

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

adding some javascript: 添加一些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; canPerformAction应该在你的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); 为UIWebView定义一个类别(如果可以从所有UIWebView中删除此行为);

  2. derive your own web view class from UIWebView and override that method in there. UIWebView派生自己的Web视图类并在那里覆盖该方法。

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

还将禁用Mobile Safari上的表单。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM