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

csc*_*uff 61 email ios

我发现的是

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

Vla*_*mir 81

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

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

  • 想知道Slack应用程序是否使用这种技术.它们在没有邮件撰写视图的情况下启动 (3认同)
  • 这应该是公认的答案.当前接受的答案说这是不可能的,但是使用"message //"会打开没有撰写屏幕的电子邮件应用程序. (2认同)

Ami*_*mit 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]];
Run Code Online (Sandbox Code Playgroud)

  • 这将在撰写视图中打开. (15认同)

Voj*_*vik 19

Swift原版Amit的回答:

斯威夫特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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个版本实际上对我不起作用.似乎只有主题和正文应该根据Apple的doc在这里编码:https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MailLinks/MailLinks.html因此答案中正确的`urlString`应该是是`let urlString ="mailto:\(toEmail)?subject = \(subject.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)&body = \(body.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)"`.可以重构此代码以更好地格式化和更安全的代码. (4认同)

Bri*_*ggs 11

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

  • 请参阅成功回答问题的备选答案:http://stackoverflow.com/a/29212029/6426003 (2认同)

Jes*_* S. 10

您可以打开邮件应用程序,而无需使用url方案打开撰写视图 message://


cat*_*ore 9

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

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"your@email.com"]];
Run Code Online (Sandbox Code Playgroud)

请注意,此行对模拟器不起作用.

  • +1注意到这在模拟器上不起作用 (9认同)

Dio*_*nes 7

如果您知道其URL方案,则可以在iOS上启动任何应用程序.不知道Mail app方案是公开的,但你可以偷偷摸摸地试试这个:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"message:message-id"]];
Run Code Online (Sandbox Code Playgroud)

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


Sta*_*kov 5

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

//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]];
Run Code Online (Sandbox Code Playgroud)