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?
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?
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];
}
message://http://domain/file.msg
. –
Kemeny 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]];
stringByAddingPercentEscapesUsingEncoding
is encoding all special characters (including the colon after mailto
). The solution is either to find a better encoding function or get rid of the encoding line and manually encode the message by substituting %20 for the spaces in the subject
and body
. –
Southland 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)
}
}
urlString
in the answer should be let urlString = "mailto:\(toEmail)?subject=\(subject.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)&body=\(body.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)"
. This code can be refactored to better format and safer code though. –
Veronicaveronika You can open the mail app without using opening the compose view by using the url scheme message://
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.
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.
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
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]];
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);
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)
Swift 5 version:
if let mailURL = URL(string: "message:") {
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
}
On swift 2.3: open mailbox
UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)
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];
}
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
In Swift:
let recipients = "[email protected]"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)
© 2022 - 2024 — McMap. All rights reserved.