在 UIWebView 中禁用用户选择 [英] Disabling user selection in UIWebView

查看:37
本文介绍了在 UIWebView 中禁用用户选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我可以在其中将内容加载到 UIWebView 并呈现它.我无法完全禁用用户交互,因为我希望用户能够单击链接.我只需要禁用用户选择.我在 Internet 上的某个地方找到了您可以使用的内容:

I have an app where I load content to a UIWebView and present this. I cannot disable user interaction completely because I want the user to be able to click links. I just need to disable user selection. I found somewhere in the Internets that you can use:

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

我尝试将其插入为

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

webViewDidFinishLoad:

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

However, it does not work. I am still able to select and copy text inside the WebView.

有什么想法可能出问题了吗?

Any Ideas what might be going wrong?

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

Update: This only seems to happen starting with iOS 4.3

推荐答案

这里有几种禁用选择的方法:

Here are a few ways to disable selection:

<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];
}

这篇关于在 UIWebView 中禁用用户选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆