0

I'm trying to implement a mailto share link into my iOS application. But facing an issue when using mailto: URL scheme.

I tried to use tel:+1234567890 URL scheme and it's working properly. I tried running the app on iOS 13 on a device (not simulator), and Mail app is currently installed with a mailbox configured.

Here is the code:

    //let urlString = String("mailto:test@.example.com") EDITED
    let urlString = String("mailto:test@example.com")
    if let url = URL(string: urlString) {
        if(UIApplication.shared.canOpenURL(url)){
            print("ok")
        }else{
            print("not ok")
        }
    }

    if let url = URL(string: urlString) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:])
        }else{
            UIApplication.shared.openURL(url)
        }
    }

UIApplication.shared.canOpenURL(url) returns false.

Here is the error returned by UIApplication.shared.open method:

2020-01-14 14:17:42.497160+0100 MIPMobile[28645:9005290] [default] Failed to open URL email+launcher://?url=mailto:test@.example.com: Error Domain=NSOSStatusErrorDomain Code=-10814 "(null)" UserInfo={_LSLine=247, _LSFunction=-[_LSDOpenClient openURL:options:completionHandler:]}

I don't think that the problem occurred prior to iOS. Does anyone know the reason for this issue?

Thank for your help

benmel
  • 9
  • 1
  • 3
  • 4
    Hi, why don't you use MFMailComposeViewController to open the Mail App ? – Daniel Ghrenassia Jan 14 '20 at 13:51
  • If you really want to use mailto you should see this: https://stackoverflow.com/a/25991205/9577134 :) – Daniel Ghrenassia Jan 14 '20 at 13:53
  • Hi @danielg, Thank for your messages. Using MFMailComposeViewController seems to be more a workaround, it does not explain why `UIApplication.shared.open(url)` method raises this error. I'll continue to investigate and have a look this workaround. – benmel Jan 14 '20 at 14:07
  • It's weird, I've just copy and paste you code and it prints "ok" on my iPhone so it should work .. I'm on iOS 13.3.1 with an iPhone X, what are you using ? – Daniel Ghrenassia Jan 14 '20 at 14:45
  • Are you running this code in the simulator? Also, did you mean to put a `.` immediately after the `@` sign in your test email address? Your code executes as desired when I run it on 13.3 as well – rolling_codes Jan 14 '20 at 14:53
  • I've tested your code and it worked on my iPhone with iOS 13.3 - even if the email address is incorrect like in your example "mailto:test@.example.com" (the "." is superfluous) - so give it a try again. – Jochen Holzer Jan 14 '20 at 14:55
  • Hello, Thank for your replies. Yes, the "." is superfluous, just a typo error it's not the issue cause. I'm using an iPhone Xr on iOS13.3 and another one on iOS13.1.2. I tried to use MFMailComposeViewController and its method canSendEmail() also returns false. As it is an enterprise app, I think that the root cause come from our MDM (bad policy configuration), but error message is weird and doesn't help... I'll check with our administrators in the next days. – benmel Jan 14 '20 at 16:41
  • Hi everybody, For information error was caused by our MDM policy configuration. Thank for you help. – benmel Jan 20 '20 at 10:31

0 Answers0