Launch Apple Mail App from within my own App?

2020-01-26 05:17发布

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?

标签: ios email
15条回答
成全新的幸福
2楼-- · 2020-01-26 05:58

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];
}
查看更多
ゆ 、 Hurt°
3楼-- · 2020-01-26 06:03

You can open the mail app without using opening the compose view by using the url scheme message://

查看更多
beautiful°
4楼-- · 2020-01-26 06:05

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)
    }
}
查看更多
登录 后发表回答