3

OK, the scenario is simple:

  • I have a WebView
  • I want the user not to be able to cut/copy anything from that webview, no matter what (either with ⌘C, or via the Edit menu)

I know I have to subclass WebView, but which specific methods do I have to override?

Any ideas? (Any other approach is welcome!)

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223

2 Answers2

4

Add the following CSS to some file

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

And link that CSS to your 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>  

Or programmatically disable it

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}
l0gg3r
  • 8,864
  • 3
  • 26
  • 46
  • Hmm... I've seen a similar answer, like 100 times, but it most definitely has no effect at all. I can perfectly go inside the Webview, pick some text and Cmd+Copy it, or use the Edit menu... (I really have no idea what is going on, or why what supposedly works for others, doesn't work for me!) – Dr.Kameleon Nov 11 '14 at 15:07
  • that's strange, first solution is now working on my project, if there is no selection, you can't copy it. How you're trying to copy it? – l0gg3r Nov 11 '14 at 15:09
  • Cmd+C and **Edit** > **Copy** both work. The thing is (perhaps because my situation has some twist), I want to block/intercept the `copy:` and `cut:` actions. – Dr.Kameleon Nov 11 '14 at 15:25
  • http://stackoverflow.com/questions/5995210/disabling-user-selection-in-uiwebview "Disable the Copy / Paste user menu:" also doesn't work? – l0gg3r Nov 11 '14 at 15:32
  • Nope, it doesn't. `canPerformAction:` is never getting called (I believe someone mentioned its purpose is to check for JS-triggered actions only... :S) – Dr.Kameleon Nov 11 '14 at 15:34
  • You need to swizzle `-[UIWebDocumentView canPerformAction:]` with that one, did you? – l0gg3r Nov 11 '14 at 15:36
  • Just found the answer. I'm posting it right now ! :-) – Dr.Kameleon Nov 11 '14 at 15:57
3

OK, here's the solution.

First, set the Webview's Editing Delegate:

[_myWebview setEditingDelegate:self];

Then implement the one function we need in order to intercept the copy/cut actions (or any action for that matter, but that's what we're going to do anyway):

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

I hope you won't waste as much time as I did looking for a valid answer... :-)

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223