我如何在 swift 中使用在新标签页中打开?(WEBKIT)

How can i use open in new tab in swift ?(WEBKIT)

如何在我的 webkit 应用的新标签页中使用 safari-open

我正在创建一个互联网浏览器,我需要它

和照片一样 例如,当我将手指放在 Google 中的 link 上时,我想显示在新选项卡中打开选项 enter image description here

class ViewController: UIViewController, UITextFieldDelegate, WKNavigationDelegate {
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var forwardButton: UIButton!
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var urlTextField: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    urlTextField.delegate = self
    webView.navigationDelegate = self
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear( animated )
    let urlString:String = "https://www.google.com"
    let url:URL = URL(string: urlString)!
    let urlRequest:URLRequest = URLRequest(url: url)
    webView.load(urlRequest)
    urlTextField.text = urlString
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    let urlString:String = urlTextField.text!
    let url:URL = URL(string: urlString)!
    let urlRequest:URLRequest = URLRequest(url: url)
    webView.load(urlRequest)

    textField.resignFirstResponder()

    return true
}

@IBAction func backButtonTapped(_ sender: Any) {
    if webView.canGoBack {
        webView.goBack()
    }
}
@IBAction func forwardButtonTapped(_ sender: Any) {
    if webView.canGoForward {
        webView.goForward()
    }
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    backButton.isEnabled = webView.canGoBack
    forwardButton.isEnabled = webView.canGoForward

    urlTextField.text = webView.url?.absoluteString
}

}

使用下面的委托方法:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.targetFrame == nil {
            if let url = navigationAction.request.url {
                let app = UIApplication.shared
                if app.canOpenURL(url) {
                    app.open(url, options: [:], completionHandler: nil)
                }
            }
        }
        decisionHandler(.allow)
    }

可以吗?