What I already found is
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"mailto:\"]];
But I just want to open the Mail ap
Swift version of the original Amit's answer:
Swift 2:
func openMailApp() {
let toEmail = "stavik@outlook.com"
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)
}
}
Swift 3.0:
func openMailApp() {
let toEmail = "stavik@outlook.com"
let subject = "Test email".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
if let
urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)",
url = URL(string:urlString) {
UIApplication.shared().openURL(url)
}
}
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&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]];
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)
In Swift:
let recipients = "someone@gmail.com"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)
If you are using Xamarin to developer an iOS application, here is the C# equivalent to open the mail application composer view:
string email = "yourname@companyname.com";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
on swift 2.3: open mailbox
UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)