简体   繁体   English

可可制作WKWebView命令+链接单击在新窗口中打开

[英]Cocoa make WKWebView command + link click open in new window

I'm trying to make WKWebView open a link in a new window/tab when user has the cmd key pressed when clicking on a link, just like in any browser. 我试图使WKWebView在用户单击链接时按下cmd键时在新窗口/选项卡中打开链接,就像在任何浏览器中一样。 I couldn't find a native api to do so (or maybe am I just suck at googling). 我找不到这样做的本机api(或者我只是在谷歌搜索上很烂)。 Can anyone give me an idea how I can implement this with no dirty hacks? 谁能给我一个主意,我该如何在没有恶意黑客的情况下实现这一目标? Did I miss a naive api? 我想念天真的api吗?

Use a WKNavigationDelegate . 使用WKNavigationDelegate

In the navigation process, the 在导航过程中,

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

method will be called. 方法将被调用。 The WKNavigationAction object has a modifierFlags property that tells you whether the cmd key was pressed during the clic. 该WKNavigationAction对象有一个modifierFlags属性,告诉您该CLIC期间是否按下命令键。

If it was, then do whatever you wish to open a new window/tab with a new WKWebView and start loading the page from here (I personally send an event with all the info that will be processed later on). 如果是这样,请执行任何操作以打开带有新WKWebView的新窗口/选项卡,然后从此处开始加载页面(我个人发送了一个事件,其中包含所有稍后将要处理的信息)。

Finally, cancel you current web view navigation by calling 最后,通过调用取消当前的网络视图导航

decisionHandler(WKNavigationActionPolicyCancel) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM