mailto: link in UIWebView - Does Not work

前端 未结 4 1624
猫巷女王i
猫巷女王i 2021-02-12 12:37

Is the areanything special you need in html or Callbacks in a UIWebView to handle anchor tags with an href, or is there something special about an anchor tag with a mailto link

4条回答
  •  太阳男子
    2021-02-12 12:55

    Working example for Swift 4: 3 cases are treated, expand as needed.

    1. mailto-link is working
    2. some URL is clicked that you want to open in Webview itself
    3. open all other URLs in mobile safari

      func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
      
      if let url = navigationAction.request.url {
          if (url.scheme == "mailto") {
              // mailto: link is clicked
              UIApplication.shared.open(url)
              decisionHandler(.cancel)
              return
          }
          if (url.absoluteString.contains("www.example.com/webviewURL")) {
              // Load this stuff in WebView
              decisionHandler(.allow)
              return
          } else {
              // open any other URL in mobile Safari
              UIApplication.shared.open(url)
              decisionHandler(.cancel)
              return
          }
      }
      
      decisionHandler(.cancel)
      return
      }
      

提交回复
热议问题