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

标签 swift webview whatsapp mailto tel

我是 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
    }
}

输出:

enter image description here

关于swift - 如何在 Swift webview 中打开外部链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69987992/

相关文章:

swift - navigationController?.navigationBar.isUserInteractionEnabled 没有按预期工作

Android:相机预览上的透明 WebView

java - WebView 播放 Flash 文件时出现问题

android - 如何在 Whatsapp 中将文本设置为描述(关于)?

url - 通过 URL 向多个联系人发送 WhatsApp 消息

ios - 如何通过触摸单元格内的按钮快速编辑文本字段

iOS/swift : Record audio with 16khz on every hardware using AVAudioEngine

ios - 在 iOS 上检查位置服务权限

安卓 WebView : Mailto Unable to Implement

node.js - 错误 : Failed to launch the browser process! Whatsapp web js | Azure 门户 Linux puppeteer 师