How can my Cocoa application be notified of onClick events in a WebView?

wkwebview objective-c
ios webview example
wkwebviewconfiguration

I'm loading a WebView in my OSX application and want to receive a notification in my app when the user clicks on an element in the WebView. (The WebView is loading a web page that is part of my web application.)

Is this possible?

I can think of a way to do it (poll a javascript value that gets set on the click event) but I'm hoping there's a more sensible way!


Since the web page is your web application, then I assume you have control over the html.

Use WebScript.

Here's a little demo:

Objective-C:

- (void)windowDidLoad{
    [[webView windowScriptObject] setValue:self forKey:@"objcConnector"];
    ....
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector{
    if (aSelector == @selector(elementClicked:)) return NO;
    return YES;
}
-(void)elementClicked:(id)object{
    //object is the id of the element
}

HTML:

<button id="example" onClick="window.objcConnector.elementClicked_(this.id)">Click me</button>

Cocoa and Objective-C Cookbook, I'm loading a WebView in my OSX application and want to receive a notification in my app when the user clicks on an element in the WebView . (The WebView is​  I think I can manage to develop the logic to use the webview I currently have. I just can´t seem to find anywhere on the web a tip for creating a method to "listen" to the user click and catch information (or just the ID) of the notification created, so I can build the routine. Anyone can help or have a better solution? Thanks, Jorge Mauricio


I think this delegate method should be useful. Open a link from the WebView would call this method, so you can figure out what to do with it.

- (void)webView:(WebView *)sender
        decidePolicyForNavigationAction:(NSDictionary *)actionInformation
        request:(NSURLRequest *)request frame:(WebFrame *)frame
        decisionListener:(id<WebPolicyDecisionListener>)listener

Or you can listen to the WebView frame change URL :

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame 

The Complete Idiot's Guide to iPad and iPhone App Development: , Click the Build and Runtoolbar buttoninXcodeand let's see our web view in action​. Since Cocoa's WebView is based on WebKit, you can provide similar functionality to what is available in the By implementing just afew of WebView's delegate methods, our applicationcan be notified whenthe titleelement hasloaded and  You can be notified about clicking on links like so: webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // The webView is about to navigate to the specified url.


Subclass NSWindow and catch the mouse down events on the WebView which is actually a WebHTMLView:

@interface MyWindow : NSWindow
@end

@implementation MyWindow

- (void)sendEvent:(NSEvent *)event
{

    if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown)
    {
        NSView *deepView = [[self contentView] hitTest:[event locationInWindow]];
        if([[deepView className] isEqualToString:@"WebHTMLView"]){
            return;
        }
    }
    [super sendEvent:event];

}

@end

Then by monitoring the mouse over element delegate method you can track which element was clicked.

Learn OS X Lion, Turn Your Cool Idea into the Next Hot App Troy Brant Starting and stopping the spinner is very simple, and you will see how it's done in the code in the next section. Builder, then add the code to launch the bookmark list in an action sheet. to the bottom of your interface using the Library > Cocoa Touch > Windows,  b123400. Looking for jobs in London. 20. cocoa-touch. Score 122. Posts 3. uiimage. 5 How can my Cocoa application be notified of onClick events in a WebView?


Malhal's solution did not work for me in Mac OS. WebView in Mac OS is weird and wkWebView is incomplete.

Having set the first responder to the webView, this one did:

- (void)sendEvent:(NSEvent *)event
{

if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown)
{

    if([[self.firstResponder className] isEqualToString:@"WebHTMLView"])
    {
        NSLog(@"MouseDown at: %f, %f",event.locationInWindow.x,event.locationInWindow.y);
        return;
    }
}
[super sendEvent:event];

}

Using the WebView - Cocoa and Objective-C Cookbook, your Text Field to your Web View so that when you enter a web address in your select your text field object; then, while holding the Control key, click and drag from View, release the button, and a menu will pop up asking you which action you to your application without much trouble (i.e., connecting a Button object to​  The WebView is used to embed web content in your application. Using a WebView, you can build advanced web browser functionality in your application. This website uses cookies and other tracking technology to analyse traffic, personalise ads and learn how we can improve the experience for our visitors and customers.


Learning iPhone Programming: From Xcode to App Store, The WebView is used to embed web content in your application. Right-click on Frameworks folder in the Project view of Xcode and choose Add, Click the Build and Run toolbar button in Xcode and let's see our web view in action. just a few of WebView's delegate methods, our application can be notified when the title  If you try to use window.external.notify () in your WebView not only do you need the link to be in HTTPS but also need to declare the https link (s) in the manifest of your application and need to have access to the html page to add IE/EDGE specific code.


Android webview url click event, From Xcode to App Store Alasdair Allan Three files will be created: the interface file WebViewController.h, the implementation file WebViewController.m, and  WebView with Load URL and Bind JavaScript to Android Handling Page Navigation. If you click the link in the web page by default it will launch the web browser and loads the appropriate URL. so we have to create a client for override this behavior to load the clicked link within your WebView and also can be navigate backward and forward through the web page history that’s maintain by your


WebView, Fix android webview unit tests to dispatch correct click event. Jun 21, 2018 · Home » Java » My android application is opening a webview in which when I click on You can create WebView based app in just few minutes but the condition is Notice: make sure you're in Text mode when editing The webview API allows  Although using a WebView degrades the user experience of the application, there are some places where we can’t do without it. So despite not wanting to, we end up using WebViews in our application.