如何在uiwebview中选择文本时禁用复制共享选项?但是选择文本应该有效

时间:2016-06-23 08:55:04

标签: ios uiwebview selection copy-paste

我想限制用户在UIWebView中复制和共享文字。

我使用的代码是

- (void)viewDidLoad {
[super viewDidLoad];
self.webview.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleHTML" ofType:@"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlContent baseURL:[NSURL URLWithString:@""]];

// Do any additional setup after loading the view, typically from a nib.

}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:))
return NO;

else if (action == @selector(select:))
return YES;

return [super canPerformAction:action withSender:sender];
}

2 个答案:

答案 0 :(得分:3)

您需要做的第一件事是添加具有UIWEBVIEW子类的新类。

将此代码粘贴到班级的.m文件中。

@implementation UIWebView (Additional)

- (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:)||
            action == @selector(_share:))
        {
            return false;
        }
    }
    return superCanPerform;
}

尝试这一点,这将隐藏所需的所有子菜单。

答案 1 :(得分:0)

尝试使用代码,这对我有用

Request.tag

禁用共享,

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

希望这对你有用。