在Safari中打开WebView链接 [英] Opening WebView links in Safari
问题描述
我在Cocoa开发一个应用程序,我想知道是否有一种方法/如何使任何链接点击在我的webview中启动在Safari(或默认浏览器)。提前感谢!
I am developing an application in Cocoa, and I would like to know if there is a way/how to make any link clicked in my webview to launch in Safari (or the default browser). Thanks in advance!
推荐答案
我刚刚碰到这个问题,并以这种方式解决:
I just ran into this problem myself, and solved it this way:
我的主控制器对象被设置为WebView的policyDelegate。
My main controller object was set as the policyDelegate for the WebView.
然后实现这个方法:
- (void)webView:(WebView *)webView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener
{
NSString *host = [[request URL] host];
if (host) {
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
} else {
[listener use];
}
}
我需要检查主机,因为否则当我加载WebView的初始内容(在我的项目中只是一个静态html页面),它是在Safari中启动。现在,只有与实际主机的链接(外部链接)被发送到Safari,这是我想要的行为。我想我也可以没有设置policyDelegate,直到加载初始页面。
I needed to have the check for the host there, because otherwise when I load the initial content of the WebView (just a static html page in my project), it was being launched in Safari. Now, just links with actual hosts (external links) get sent to Safari, which is the behavior I wanted. I think I could also have not set the policyDelegate until after the initial page was loaded.
这篇关于在Safari中打开WebView链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!