我可以通过将我的 webView 设置为 PolicyDelegate 来解决我的问题。
[webView setPolicyDelegate:self];
并从以下代码实现: pandoraboy
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
// HACK: This is all a hack to get around a bug/misfeature in Tiger's WebKit
// (should be fixed in Leopard). On Javascript window.open, Tiger sends a null
// request here, then sends a loadRequest: to the new WebView, which will
// include a decidePolicyForNavigation (which is where we'll open our
// external window). In Leopard, we should be getting the request here from
// the start, and we should just be able to create a new window.
WebView *newWebView = [[WebView alloc] init];
[newWebView setUIDelegate:self];
[newWebView setPolicyDelegate:self];
return newWebView;
}
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
if( [sender isEqual:webView] ) {
[listener use];
}
else {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
[sender release];
}
}
- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id<WebPolicyDecisionListener>)listener {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
}
我希望这也可以帮助其他人。:)