如何快速使用新标签中的打开?(WEBKIT)

时间:2019-09-16 10:15:55

标签: swift webview wkwebview

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

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

就像照片一样 例如,当我将手指放在Google中的链接上时,我想显示“在新标签页中打开”选项 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
}

}

1 个答案:

答案 0 :(得分:0)

在您上次使用此方法时使用此委托方法:

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)
    }

可以吗?