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

How to disable copy share option while selecting text in uiwebview? But selecting text should work

我想限制用户在 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];
}

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

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

要禁用共享,

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
if (action == @selector(customMethod:)) {
    return [super canPerformAction:action withSender:sender];
}
return NO;
}

希望这对你有用。

您需要做的第一件事是添加新的 class 和 UIWEBVIEW 的 subclass。

将此代码粘贴到 class.

的 .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;
}

试试这个,它会隐藏所有需要的子菜单。