Why is WKWebView not opening links with target="_blank"? Ask Question

Why is WKWebView not opening links with target=

WKWebView does not open any links which have target="_blank" a.k.a. 'Open in new Window' attribute in their HTML <a href>-Tag.

ベストアンサー1

私の解決策は、ナビゲーションをキャンセルし、loadRequest: でリクエストを再度ロードすることです。これにより、現在のフレームで常に新しいウィンドウを開く UIWebView と同様の動作になります。

デリゲートを実装しWKUIDelegate、 に設定します_webview.uiDelegate。次に、以下を実装します。

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
  if (!navigationAction.targetFrame.isMainFrame) {
    [webView loadRequest:navigationAction.request];
  }

  return nil;
}

おすすめ記事