如何在 Swift webview 中打开外部链接?

How can I open external links in Swift webview?

我是 Swift 的新手,我只需要我网站的 webview 应用程序。除了“tel”、“mailto”或“whatsapp”等外部链接外,一切似乎都很好。当我点击它们时,没有任何反应。我已经搜索了许多相关的标题并尝试了很多东西,但链接仍然无效。我最后尝试的是下面。有人可以帮忙吗?


import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
    
    var webView: WKWebView!
    
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myURL = URL(string:"https://www.sepettte.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
      
      guard let failingUrlStr = (error as NSError).userInfo["NSErrorFailingURLStringKey"] 
      as? String  else { return }
      let failingUrl = URL(string: failingUrlStr)!

      switch failingUrl {
      
        case _ where failingUrlStr.hasPrefix("fb:"):
        if #available(iOS 10.0, *) {
           UIApplication.shared.open(failingUrl, options: [:], completionHandler: nil)
           return
        }
          
        case _ where failingUrlStr.hasPrefix("whatsapp:"):
        if UIApplication.shared.canOpenURL(failingUrl) {
          UIApplication.shared.open(failingUrl, options: [:], completionHandler: nil)
          return
        }

        case _ where failingUrlStr.hasPrefix("mailto:"):
        if UIApplication.shared.canOpenURL(failingUrl) {
          UIApplication.shared.open(failingUrl, options: [:], completionHandler: nil)
          return
        }
          
        case _ where failingUrlStr.hasPrefix("tel:"):
        if UIApplication.shared.canOpenURL(failingUrl) {
          UIApplication.shared.open(failingUrl, options: [:], completionHandler: nil)
          return
        }

      default: break
      }
    }
}

尝试在实现 decidePolicyFor navigationAction 的 WKWebView 上拦截请求:

class ViewController: UIViewController, WKNavigationDelegate {
    
    @IBOutlet weak var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        webView.uiDelegate = self
        webView.navigationDelegate = self

        webView.allowsBackForwardNavigationGestures = true

        let myURL = URL(string:"https://www.sepettte.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
        
        guard let redirectURL = (navigationAction.request.url) else {
            decisionHandler(.cancel)
            return
        }
        
        if (redirectURL.absoluteString.contains("tel:") ) {
            UIApplication.shared.open(redirectURL, options: [:], completionHandler: nil)
        }
        
        if (redirectURL.absoluteString.contains("whatsapp") ) {
            UIApplication.shared.open(redirectURL, options: [:], completionHandler: nil)
        }
        
        decisionHandler(.allow)
    }
}
extension ViewController: WKUIDelegate {

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        
        guard let url = navigationAction.request.url else {
            return nil
        }
        
        guard let targetFrame = navigationAction.targetFrame, targetFrame.isMainFrame else {
            webView.load(URLRequest(url: url))
            return nil
        }
        return nil
    }
}

输出: