Launch Apple Mail App from within my own App?
IosEmailIos Problem Overview
What I already found is
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
But I just want to open the Mail app not only a composer view. Just the mail app in its normal or last state.
Any ideas?
Ios Solutions
Solution 1 - Ios
Apparently Mail application supports 2nd url scheme - message://
which ( I suppose) allows to open specific message if it was fetched by the application. If you do not provide message url it will just open mail application:
NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
[[UIApplication sharedApplication] openURL:mailURL];
}
Solution 2 - Ios
NSString *recipients = @"mailto:[email protected][email protected],[email protected]&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
Solution 3 - Ios
Swift version of the original Amit's answer:
Swift 5
func openEmailApp(toEmail: String, subject: String, body: String) {
guard
let subject = subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
else {
print("Error: Can't encode subject or body.")
return
}
let urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)"
let url = URL(string:urlString)!
UIApplication.shared.open(url)
}
Swift 3.0:
func openMailApp() {
let toEmail = "[email protected]"
let subject = "Test email".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
if
let urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)",
let url = URL(string:urlString)
{
UIApplication.shared().openURL(url)
}
}
Swift 2:
func openMailApp() {
let toEmail = "[email protected]"
let subject = "Test email".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()
let body = "Just testing ...".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()
if let
urlString = ("mailto:\(toEmail)?subject=\(subject)&body=\(body)")),
url = NSURL(string:urlString) {
UIApplication.sharedApplication().openURL(url)
}
}
Solution 4 - Ios
You can open the mail app without using opening the compose view by using the url scheme message://
Solution 5 - Ios
Since the only way to launch other applications is by using their URL schemes, the only way to open mail is by using the mailto: scheme. Which, unfortunately for your case, will always open the compose view.
Solution 6 - Ios
Run your app on a real device and call
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"[email protected]"]];
Note, that this line takes no effect on simulator.
Solution 7 - Ios
You can launch any app on iOS if you know its URL scheme. Don't know that the Mail app scheme is public, but you can be sneaky and try this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"message:message-id"]];
Props to Farhad Noorzay for clueing me into this. It's some bit of reverse engineering the Mail app API. More info here: https://medium.com/@vijayssundaram/how-to-deep-link-to-ios-7-mail-6c212bc79bd9
Solution 8 - Ios
Expanding on Amit's answer: This will launch the mail app, with a new email started. Just edit the strings to change how the new email begins.
//put email info here:
NSString *toEmail=@"[email protected]";
NSString *subject=@"The subject!";
NSString *body = @"It is raining in sunny California!";
//opens mail app with new email started
NSString *email = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@", toEmail,subject,body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
Solution 9 - Ios
If you are using Xamarin to developer an iOS application, here is the C# equivalent to open the mail application composer view:
string email = "[email protected]";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
Solution 10 - Ios
Swift 4 / 5 to open default Mail App without compose view. If Mail app is removed, it automatically shows UIAlert with options to redownload app :)
UIApplication.shared.open(URL(string: "message:")!, options: [:], completionHandler: nil)
Solution 11 - Ios
Swift 5 version:
if let mailURL = URL(string: "message:") {
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
}
Solution 12 - Ios
On swift 2.3: open mailbox
UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)
Solution 13 - Ios
It will open Default Mail App with composer view:
NSURL* mailURL = [NSURL URLWithString:@"mailto://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
[[UIApplication sharedApplication] openURL:mailURL];
}
It will open Default Mail App:
NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
[[UIApplication sharedApplication] openURL:mailURL];
}
Solution 14 - Ios
You might want to use a scripting bridge. I used this method in my App to directly give the user the option to send e-mail notifications using the built-in Mail.app
. I also constructed an option to do this directly over SMTP as an alternate.
But since you want to use Mail.app
method, you can find more information about how to do that solution by following this:
https://github.com/HelmutJ/CocoaSampleCode/tree/master/SBSendEmail
Solution 15 - Ios
In Swift:
let recipients = "[email protected]"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)