Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when link is clicked WebView (Mac)

I'm trying to detect when I click a link in a WebView. I set the delegate with

[_WebCont delete:self];
_WebCont.policyDelegate = self;

and

WebPolicyDecisionListener

I'm using this as my listener:

- (void)_WebCont:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
    NSString *url = request.URL.absoluteString;
    url = [NSString stringWithFormat:@"*load %@",url];
    [self sendMessage:url];
    NSLog([NSString stringWithFormat:@"Click: %@",url]);

}

But when ever I click a link in the view it never logs anything. Any help on fixing this?

like image 307
Allison Avatar asked Feb 18 '23 05:02

Allison


1 Answers

To implement a policy listener you need do this:

WebPolicyDecisionListener.h:

// ...
@interface WebPolicyDecisionListener: NSObject {
// ...
}


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

-(void)webView:(WebView *)webView decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
                                                         request:(NSURLRequest *)request
                                                    newFrameName:(NSString *)frameName
                                                decisionListener:(id <WebPolicyDecisionListener>)listener;
// ...
@end

WebPolicyDecisionListener.m

@implementation WebPolicyDecisionListener
// ...
- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation
                                                           request:(NSURLRequest *)request
                                                             frame:(WebFrame *)frame
                                                  decisionListener:(id<WebPolicyDecisionListener>)listener
{
  if (WebNavigationTypeLinkClicked == [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue])
  {
    // link was clicked do something with it...
  }
  [listener use]; // Say for webview to do it work...
}


-(void)webView:(WebView *)webView decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
                                                         request:(NSURLRequest *)request
                                                    newFrameName:(NSString *)frameName
                                                decisionListener:(id <WebPolicyDecisionListener>)listener
{
  if (WebNavigationTypeLinkClicked == [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue])
  {
    // link was clicked and webview want to open it in new window do something with it...
  }
  [listener ignore]; // ignore webview default implementation...
}
// ...
@end

and somewhere in your code something like this:

WebPolicyDecisionListener* listener = [[WebPolicyDecisionListener alloc] init];
// ...
[_WebCont setPolicyDelegate:listener];
like image 50
PSyton Avatar answered Mar 07 '23 22:03

PSyton