mailto: link in UIWebView - Does Not work
Asked Answered
E

4

21

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 in the href?

Elbert answered 5/4, 2010 at 17:29 Comment(0)
A
64

In your UIWebView's delegate, do:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[[request URL] scheme] isEqual:@"mailto"]) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    return YES;
}
Autonomic answered 5/4, 2010 at 23:33 Comment(1)
In Swift 4 func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if (request.url?.scheme == "mailto") { UIApplication.shared.open(request.url).... Biographer
K
35

I noticed that when running in the iPhone simulator, I could click on an HTTP link in my UIWebView and it would open in Safari, but when I clicked on a mailto link nothing would happen. But when I deployed the app to a real device, it worked. I expect the reason is because there is no mail app on the simulator, but there is Safari.

Kopeck answered 22/10, 2010 at 20:6 Comment(1)
No email app on the iOS Simulator. D'oh! That never crossed my mind. Thanks for ending my confusion and frustration.Mcclimans
A
2

I wanted to note that this is probably not the best solution.

UIWebView does have a property called dataDetectorTypes - setting this to all or address should solve your problem without overriding the Method mentioned above:

myWebView.dataDetectorTypes = UIDataDetectorTypeAll; //or UIDataDetectorTypeAddress etc..

But be aware that this might not work on the simulator - I tried it with the iPad 6.0 Simulator where it's not working although on an iPad Device it's working like a charm!

Aslant answered 7/11, 2012 at 7:31 Comment(0)
R
2

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
    }
    
Rupiah answered 29/1, 2019 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.