0
votes

I am working on a project using Objective C on macOS. The project is Document-Base Application. Each window contains a WebView. I want to implement copy: cut: and paste: where the menu items for these actions should be enabled/disabled according to the object selected inside the WebView. I started a new Document-Base Application and added the following code inside AppDelegate.m:

- (void)copy:(id)sender {
    NSLog(@"%@ %s", self, __func__);
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {

    NSString *title = [menuItem title];
    NSLog(@"%@",title);
    return YES;
}

Before I add the WebView to the Document.xib file, everything is working fine and both copy and validateMenuItem functions are called normally. When I add WebView to the Document.xib file, these function stop working. I tried many things (such as override the WebView class) without any success. I know WebView is deprecated, but using WKWebView will cause other issues.

1

1 Answers

1
votes

The WebHTMLView inside the WebView is the target for the copy: action. The editingDelegate of the WebView can intercept the copy: action. Implement

- (BOOL)webView:(WebView *)webView doCommandBySelector:(SEL)selector;

The UIDelegate of the WebView can validate the Copy menu item.

- (BOOL)webView:(WebView *)webView validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item defaultValidation:(BOOL)defaultValidation;