124

我有一个应用程序,我将内容加载到 aUIWebView并呈现它。我不能完全禁用用户交互,因为我希望用户能够单击链接。我只需要禁用用户选择。我在互联网的某个地方找到了可以使用的地方:

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

我尝试将其插入为

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

webViewDidFinishLoad:

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

任何想法可能会出现什么问题?

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

4

12 回答 12

281

以下是禁用选择的几种方法:

将以下内容添加到您的移动 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];
}
于 2011-05-18T21:19:46.960 回答
105

我可以确认以下代码适用于 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 及更高版本。这是快速代码:

func webViewDidFinishLoad(webView: UIWebView) {
    // Disable user selection
    webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none'")!
    // Disable callout
    webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitTouchCallout='none'")!
}
于 2012-10-02T16:29:15.487 回答
26

我在 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 */

}
于 2013-01-20T12:44:54.967 回答
18

我喜欢 WrightsCS 解决方案,但我会使用它,这样用户仍然可以使用复制、粘贴和选择输入操作

<style type="text/css">
*:not(input,textarea) {
    -webkit-touch-callout: none;
    -webkit-user-select: none; /* Disable selection/Copy of UIWebView */
}
</style>
于 2012-11-02T20:58:59.840 回答
9

我不确定设置是如何完成的,但是为什么在调用 viewWillDisappear 时不清除 pasteBoard。可能类似于您的 appDelegate.m 中的内容:

[UIPasteboard generalPasteboard].string = nil;

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

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

于 2011-05-24T20:45:08.400 回答
7

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';"];
}
于 2014-07-29T12:14:55.050 回答
7

我可以确认这肯定对你有用。

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

如果您只想禁用锚按钮标签,请使用此选项。

    a {-webkit-user-select: none; /* disable selection/Copy of UIWebView */
   -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
     }
于 2014-08-21T07:35:25.810 回答
6
    let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: nil, action: nil)
    longPress.minimumPressDuration = 0.2
    webView.addGestureRecognizer(longPress)

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

于 2017-07-21T13:14:30.567 回答
5

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

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;
}
于 2012-11-09T00:50:00.380 回答
4

给出的第一个解决方案对我来说非常有效......直到我将 .pdf 加载到我的 UIWebView 中。

加载 .doc 文件效果很好,但加载 .pdf 导致以下代码行不再具有所需的效果,并且在用户长按时再次弹出复制/定义菜单。

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

在又一次拉扯头发之后,我在 Johnny Rockex 上找到了这个答案,它就像一个冠军。 显示 PDF 文件时没有复制/粘贴的 UIWebView

非常感谢他提供了这个易于实施的天才解决方案!!

于 2014-01-01T15:39:27.007 回答
2

对我来说,我打算NSDataUIWebViewby获取图像LongPressGesture

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

我发现了这个: 在此处输入图像描述

这意味着,放大镜和复制/粘贴/剪切需要 0.5 秒来执行,所以如果你的 func 可以在 0.49 秒内执行,DONE !

self.longPressPan.minimumPressDuration = 0.3
于 2015-09-12T06:26:52.867 回答
-4

使用网页视图交互功能

   webView.userInteractionEnabled = false

这个对我有用

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

于 2015-03-13T14:05:49.400 回答