Reputation: 5542
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?
Upvotes: 72
Views: 62925
Reputation: 1616
Link("Email us", destination: URL(string: "message://")!)
If you want to customize the appearance of the tappable view, you can use the Link(destination:label:)
initializer:
Example:
Link(destination: URL(string: "message://")!) {
HStack {
Image(systemName: "mail")
Text("Email us")
}
}
Upvotes: 0
Reputation: 2482
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)
}
}
Upvotes: 21
Reputation: 330
On swift 2.3: open mailbox
UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)
Upvotes: 2
Reputation: 1
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
Upvotes: 0
Reputation: 651
Swift 5 version:
if let mailURL = URL(string: "message:") {
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
}
Upvotes: 4
Reputation: 519
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)
Upvotes: 3
Reputation: 3394
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];
}
Upvotes: 1
Reputation: 803
You can open the mail app without using opening the compose view by using the url scheme message://
Upvotes: 15
Reputation: 2757
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
Upvotes: 8
Reputation: 170859
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];
}
Upvotes: 97
Reputation: 699
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]];
Upvotes: 5
Reputation: 1297
In Swift:
let recipients = "[email protected]"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)
Upvotes: -5
Reputation: 4765
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.
Upvotes: 9
Reputation: 25270
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);
Upvotes: 3
Reputation: 1795
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]];
Upvotes: 48
Reputation: 748
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.
Upvotes: 11