LoginSignup
0
0

More than 1 year has passed since last update.

WKWebViewで2種類の"_blank"に対応した件

Posted at

対処法

target="_blank"
window.open({URL}, '_blank')
の2種類

class MainViewController: UIViewController, WKUIDelegate, WKNavigationDelegate{

    //MARK:- LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.uiDelegate = self // 追加
    }

    func webView(_ webView: WKWebView,
                 createWebViewWith configuration: WKWebViewConfiguration,
                 for navigationAction: WKNavigationAction,
                 windowFeatures: WKWindowFeatures) -> WKWebView? {
        // 変数 url にはリンク先のURLが入る
        if let url = navigationAction.request.url {
            let request = NSURLRequest(url: url)
            webView.load(request as URLRequest)
        }
        return nil
    }
}

delegateを設定する必要があり、
taget="_blank" のリンクを開こうとした時や window.open() の JavaScript が実行されるタイミングで発火する。

参考
【Swift】WKWebViewで表示しているページのtaget=”_blank”のリンク先を開く方法

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0