检测链接何时单击WebView(Mac)

时间:2012-12-09 21:27:12

标签: macos webview delegates

我正在尝试检测何时单击WebView中的链接。 我用

设置了委托
[_WebCont delete:self];
_WebCont.policyDelegate = self;

WebPolicyDecisionListener

我用它作为我的倾听者:

- (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]);

}

但是,当我点击视图中的链接时,它永远不会记录任何内容。 有任何帮助解决这个问题吗?

1 个答案:

答案 0 :(得分:7)

要实现策略侦听器,您需要执行以下操作:

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

代码中的某个地方:

WebPolicyDecisionListener* listener = [[WebPolicyDecisionListener alloc] init];
// ...
[_WebCont setPolicyDelegate:listener];