Launch Apple Mail App from within my own App?
Solution 1:
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:
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:
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:
You can open the mail app without using opening the compose view by using the url scheme message://
Solution 5:
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.