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

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

问题描述

我有一个应用程序,我将内容加载到 UIWebView 并显示此内容。我无法完全禁用用户交互,因为我希望用户能够单击链接。我只需要禁用用户选择。我在互联网中找到了你可以使用的地方:

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';"]; 

in webViewDidFinishLoad:

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

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

任何想法可能出错?

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

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代码:



Programmatically load the following Javascript code:

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



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



Disable the Copy / Paste user menu:

- (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天全站免登陆