【发布时间】:2015-01-08 03:42:46
【问题描述】:
好的,场景很简单:
- 我有一个 WebView
- 我希望用户不能够从该 web 视图中剪切/复制任何内容,无论如何(使用 ⌘C 或通过编辑菜单)
我知道我必须继承 WebView,但我必须重写哪些特定方法?
有什么想法吗? (欢迎任何其他方法!)
【问题讨论】:
标签: objective-c macos cocoa webview
好的,场景很简单:
我知道我必须继承 WebView,但我必须重写哪些特定方法?
有什么想法吗? (欢迎任何其他方法!)
【问题讨论】:
标签: objective-c macos cocoa webview
将以下 CSS 添加到某个文件中
html {
-ms-touch-action: manipulation;
touch-action: manipulation;
}
body {
-webkit-user-select: none !important;
-webkit-tap-highlight-color: rgba(0,0,0,0) !important;
-webkit-touch-callout: none !important;
}
并将该 CSS 链接到您的 HTML
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="LayoutTemplates/css/style.css" type="text/css" />
</head>
</html>
或者以编程方式禁用它
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}
【讨论】:
copy: 和 cut: 操作。
canPerformAction: 永远不会被调用(我相信有人提到它的目的是仅检查 JS 触发的操作...:S)
好的,这就是解决方案。
首先,设置 Webview 的 Editing Delegate:
[_myWebview setEditingDelegate:self];
然后实现我们需要的一个函数来拦截复制/剪切动作(或任何与此相关的动作,但这就是我们要做的):
- (BOOL)webView:(WebView *)webView doCommandBySelector:(SEL)command
{
NSString* commandStr = NSStringFromSelector(command);
if ( ([commandStr isEqualToString:@"copy:"]) ||
([commandStr isEqualToString:@"cut:"]))
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
return YES; // YES as in "Yes, I've handled the command,
// = don't do anything else" :-)
}
else return NO;
}
希望您不会像我在寻找有效答案时那样浪费太多时间... :-)
【讨论】: