iOS 14: How to send email using default email app
Letting your users send you feedback just got significantly easier with iOS 14. Let's see how.
Published: Dec. 13, 2020 Sponsored I want a Press KitPrior to iOS 14, if you wanted to let the users of your app send you an email with feedback or issues, you would likely implement the MFMailComposeViewController
which allows to send the message via Apple Mail directly from your app.
The downside is that users may not use the Apple Mail and consequently not configured. For these cases there was alternative approach where you let user select from predefined email apps (like Gmail or Outlook). Which got a bit complicated quite quickly.
I have written about this approach in the past and for older version, I think it still works pretty well.
How to handle sending email in iOS 14?
I believe the best way is to construct mailto
url and let the system handle it. It will automatically select the default email app user has selected. I think this is pretty sensible way of going about this. Small UX improvement might be to also display your support email address in the app (maybe with option to copy it) so users can use different app, or write you from their Mac.
Anyway, let's take a closer look at the mailto
url. The very basic example can look like this:
let mailtoString = "mailto:nemecek@support.com"
Btw the email address is made up.
This needs to be percent-encoded with the addingPercentEncoding
and then we would create an URL
from this and open it with UIApplication.shared.open
.
Something like this:
let mailtoString = "mailto:nemecek@support.com".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let mailtoUrl = URL(string: mailtoString!)!
if UIApplication.shared.canOpenURL(mailtoUrl) {
UIApplication.shared.open(mailtoUrl, options: [:])
}
I am using !
so the code is short and compiles when you copy it to try it. It may not be a best idea in a real app. On the other hand, we have complete control over the url and when this fails then that is a programming error. I think crash is fine here.
I have also added the canOpenURL
just in case. If that returns false, then user has no email app, not even Apple Mail set up.
Don't forget to configure your Info.plist
and add entry to mailto
under this key: LSApplicationQueriesSchemes
, so you can query it with canOpenURL
.
Configuring subject and body
This is the basic, but I guess you want to configure this a bit more. Good idea is to include the app version, along with the build number, iOS version and more to help you better troubleshoot the issues.
We can use standard URL parameters to customize the mailto url. First parameter is denoted with ?
and then others are chained with &
.
We can add a subject and body like this:
let mailtoString = "mailto:nemecek@support.com?subject=Cool app feedback&body=Hello I have an issue...".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
This will set the email app subject
field to "Cool app feedback" and the body
to "Hello I have an issue...". You can also use stuff like \n
and other special characters.
Multiple recipients, CC and BCC
We can customize the mailto url even further. If you want to add multiple recipients you can chain the addresses with ,
:
let mailtoString = "mailto:nemecek@support.com,tim@apple.com".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
You can also use the cc
parameter to include addresses in a copy field. And similarly use bcc
which works like a copy but the recipients don't see folks who were added as a bcc
.
These two work identically to the subject
and body
parameters.
So for example:
let mailtoString = "mailto:nemecek@support.com?cc=tim@apple.com&bcc=pichai@google.com".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
Don't forget to test your mailto url at least with a few email apps before you release the update 🙂 These ideally should include Apple Mail, Gmail and Outlook.
Uses: Xcode 12 & Swift 5.3