0
0

More than 3 years have passed since last update.

[iOS] [Swift] WKWebViewでtarget=“_blank”に対応する

Posted at

デリゲートを埋め込む

WKUIDelegate を埋め込む

class MyViewController: WKNavigationDelegate, WKUIDelegate {
}

_webview.uiDelegate をセットする

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.webView.uiDelegate = self
        self.webView.navigationDelegate = self
    }

webView:createWebViewWithConfiguration:メソッドを実装

対象URL(target=“_blank”)をSafariで開く

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        if navigationAction.targetFrame == nil {
            if  let url = navigationAction.request.url {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
        return nil
    }

参考

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