且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么WKWebView没有打开与target =" _blank"的链接?

更新时间:2022-02-10 08:50:21

我的解决方案是取消导航并再次使用loadRequest:加载请求。这将是类似于UIWebView的行为,它始终在当前帧中打开新窗口。

My solution is to cancel the navigation and load the request with loadRequest: again. This will be come the similar behavior like UIWebView which always open new window in the current frame.

首先,您需要实现 WKUIDelegate 。并将其设置为 _webview.UIDelegate 。然后实现:

First you need to have an implementation of WKUIDelegate. And set it to _webview.UIDelegate. Then implement:

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{

  if (!navigationAction.targetFrame.isMainFrame) {

    [webView loadRequest:navigationAction.request];
  }

  return nil;
}