在UIWebView中禁用用户select

我有一个应用程序,我把内容加载到一个UIWebView并呈现这个。 我不能完全禁用用户交互,因为我希望用户能够点击链接。 我只需要禁用用户select。 我在互联网的某个地方find了你可以使用的地方:

 document.body.style.webkitUserSelect='none'; 

我试图插入这个

 [self.contentView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitUserSelect='none';"]; 

webViewDidFinishLoad:

但是,它不起作用。 我仍然可以在WebView中select和复制文本。

任何想法可能会出错?

更新:这似乎只是从iOS 4.3开始

以下是一些禁用select的方法:

将以下内容添加到您的移动Web文档

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

以编程方式加载以下Javascript代码:

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

禁用复制/粘贴用户菜单:

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

我可以确认下面的代码在iOS 5.0 – 8.0中工作。

 - (void)webViewDidFinishLoad:(UIWebView *)webView { // Disable user selection [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"]; // Disable callout [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"]; } 

也适用于iOS 9.这是swift代码:

 func webViewDidFinishLoad(webView: UIWebView) { // Disable user selection webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none'")! // Disable callout webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitTouchCallout='none'")! } 

我在Android / iPhone的Web应用程序(与Trigger.IO打包)中使用这种技术,发现它只能用于:not()伪类的链接语法:

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

我喜欢WrightsCS解决scheme,但是我将使用它,以便用户仍然可以使用复制,粘贴和selectinput操作

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

我不知道如何设置完成,但为什么不清除viewWillDisappear时调用pasteBoard。 也许就像你的appDelegate.m:

 [UIPasteboard generalPasteboard].string = nil; 

这将确保用户可能复制的任何数据,他们将无法将其粘贴到应用程序之外。

此外,像Engin说,你可以覆盖包含uiwebview的控制器类中的canPerformSelector方法。

TPoschel答案是相当的,但在我的情况下,顺序是重要的。

 // this works - locks selection and callout - (void)webViewDidFinishLoad:(UIWebView *)webView { [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"]; [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"]; } // this doesn't work - locks only callout - (void)webViewDidFinishLoad:(UIWebView *)webView { [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"]; [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"]; } 

我可以证实这一定会为你工作。

 <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> 

如果你想只停用锚button标签使用这个。

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

一周工作的成果! 如果您想在多个页面上保存鼠标事件和用户input,所有其他答案都是不正确的。

1)Swizzle方法(由rentzsch / jrswizzle库):

 [NSClassFromString(@"UIWebDocumentView") jr_swizzleMethod:@selector(canPerformAction:withSender:) withMethod:@selector(myCanPerformAction:withSender:) error:nil]; 

NSObject的+ myCanPerformAction.h:

 @interface NSObject (myCanPerformAction) - (BOOL)myCanPerformAction:(SEL)action withSender:(id)sender; @end 

NSObject的+ myCanPerformAction.m:

 #import "NSObject+myCanPerformAction.h" @implementation NSObject (myCanPerformAction) - (BOOL)myCanPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:)) { return [self myCanPerformAction:action withSender:sender]; } if (action == @selector(paste:)) { return [self myCanPerformAction:action withSender:sender]; } return NO; } @end 

2)将UIWebView放在UIView上并添加一个代码:

  UITapGestureRecognizer* singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)] autorelease]; singleTap.numberOfTapsRequired = 2; singleTap.numberOfTouchesRequired = 1; singleTap.delegate = self; [self.view addGestureRecognizer:singleTap]; 

和这个:

 - (void)handleSingleTap:(UIGestureRecognizer*)gestureRecognizer { return; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { UITapGestureRecognizer *gesture = (UITapGestureRecognizer *)otherGestureRecognizer; if (gesture.numberOfTapsRequired == 2) { [otherGestureRecognizer.view removeGestureRecognizer:otherGestureRecognizer]; } } return YES; } 

第一个解决scheme给我完美的工作,直到我加载到我的UIWebView .pdf。

加载一个.doc文件的工作很完美,但是加载.pdf会导致下面的代码行不再具有所需的效果,并且复制/定义菜单在用户的长时间触摸中再次popup。

  [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"]; 

经过又一轮拉发,我在Johnny Rockex的这里find了答案,就像冠军一样。 显示PDF文件时没有复制/粘贴的UIWebView

非常感谢他这个容易实现,天才的解决scheme!

对于我来说,我打算通过LongPressGestureUIWebView获取图像的NSData

但放大镜和复制/粘贴/剪切总是发生在我的func执行之前。

我发现这个: 在这里输入图像描述

这意味着,放大镜和复制/粘贴/切割需要0.5s执行,所以如果你的func可以在0.49s执行,完成!

 self.longPressPan.minimumPressDuration = 0.3 
  let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: nil, action: nil) longPress.minimumPressDuration = 0.2 webView.addGestureRecognizer(longPress) 

只需将此代码添加到您的viewDidLoad()。 用户可以点击链接,但不能复制内容。

使用Web视图互动function

  webView.userInteractionEnabled = false 

它适用于我

PS:记住当你希望用户可以再次与webview进行交互时,启用交互