从我自己的应用程序中启动Apple Mail App?

时间:2012-01-11 15:31:42

标签: ios email

我已经找到的是

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];

但我只是想打开Mail应用程序而不仅仅是作曲家视图。只是正常或最后状态的邮件应用程序。

有什么想法吗?

15 个答案:

答案 0 :(得分:75)

显然,Mail应用程序支持第二个url方案 - message://,如果它被应用程序提取,我认为它允许打开特定的消息。如果您不提供消息URL,它将只打开邮件应用程序:

NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

答案 1 :(得分:47)

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]];

答案 2 :(得分:17)

原版Amit的答案的快速版本:

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)
    }
}

答案 3 :(得分:11)

由于启动其他应用程序的唯一方法是使用其URL方案,因此打开邮件的唯一方法是使用mailto:scheme。不幸的是,对于您的情况,将始终打开撰写视图。

答案 4 :(得分:9)

在真实设备上运行您的应用并致电

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"your@email.com"]];

注意,此行对模拟器没有影响。

答案 5 :(得分:9)

您可以使用网址方案message://

打开邮件应用,而无需使用打开撰写视图

答案 6 :(得分:6)

如果你知道它的URL方案,你可以在iOS上启动任何应用程序。不知道邮件应用程序是公开的,但你可以偷偷摸摸地尝试这个:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"message:message-id"]];

向Farhad Noorzay道具,告诉我这个。它对Mail app API进行了一些逆向工程。更多信息:https://medium.com/@vijayssundaram/how-to-deep-link-to-ios-7-mail-6c212bc79bd9

答案 7 :(得分:5)

扩大阿米特的答案: 这将启动邮件应用程序,并启动新电子邮件。只需编辑字符串即可更改新电子邮件的开始方式。

//put email info here:
NSString *toEmail=@"supp0rt.fl0ppyw0rm@gmail.com";
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]];

答案 8 :(得分:2)

如果您使用Xamarin开发iOS应用程序,这里是C#等效于打开邮件应用程序编辑器视图:

string email = "yourname@companyname.com";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);

答案 9 :(得分:1)

on swift 2.3:打开邮箱

UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)

答案 10 :(得分:0)

您可能要使用脚本桥。我在我的应用程序中使用了此方法,直接为用户提供了使用内置Mail.app发送电子邮件通知的选项。我还构造了一个选项,可以直接通过SMTP直接执行此操作。

但是,由于要使用Mail.app方法,因此可以按照以下步骤找到有关如何执行该解决方案的更多信息:

https://github.com/HelmutJ/CocoaSampleCode/tree/master/SBSendEmail

祝你好运!

答案 11 :(得分:0)

它将使用作曲家视图打开默认邮件应用程序:

NSURL* mailURL = [NSURL URLWithString:@"mailto://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

它将打开默认邮件应用程序:

NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

答案 12 :(得分:0)

快速4/5打开默认的Mail App,无需撰写视图。 如果邮件应用程序被删除,它将自动显示UIAlert以及重新下载应用程序的选项:)

UIApplication.shared.open(URL(string: "message:")!, options: [:], completionHandler: nil)

答案 13 :(得分:0)

Swift 5版本:

if let mailURL = URL(string: "message:") {
    if UIApplication.shared.canOpenURL(mailURL) {
        UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
    }
}

答案 14 :(得分:-5)

在斯威夫特:

let recipients = "someone@gmail.com"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)