最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable copy and paste in UIWebView - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited May 10, 2018 at 16:12 Cœur 38.7k26 gold badges203 silver badges277 bronze badges asked Jul 2, 2012 at 9:00 MutaweMutawe 6,5243 gold badges50 silver badges91 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7
-webkit-user-select: none; /* Disable selection/Copy of UIWebView */

will also disable form on Mobile Safari.

It seems it is more plex 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 */
 }
发布评论

评论列表(0)

  1. 暂无评论