4

我试图检测何时单击 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]);

}

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

4

1 回答 1

8

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

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];
于 2012-12-19T06:19:03.987 回答