Launch Apple Mail App from within my own App?

后端 未结 15 2178
心在旅途
心在旅途 2020-11-29 01:59

What I already found is

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

But I just want to open the Mail ap

相关标签:
15条回答
  • 2020-11-29 02:12

    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=@"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]];
    
    0 讨论(0)
  • 2020-11-29 02:13

    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.

    0 讨论(0)
  • 2020-11-29 02:20

    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

    0 讨论(0)
  • 2020-11-29 02:22

    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];
    }
    
    0 讨论(0)
  • 2020-11-29 02:22

    Run your app on a real device and call

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

    Note, that this line takes no effect on simulator.

    0 讨论(0)
  • 2020-11-29 02:22

    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];
    }
    
    0 讨论(0)
提交回复
热议问题