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条回答
Juvenile、少年°
2楼-- · 2020-01-26 05:49

Swift 5 version:

if let mailURL = URL(string: "message:") {
    if UIApplication.shared.canOpenURL(mailURL) {
        UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
    }
}
查看更多
Bombasti
3楼-- · 2020-01-26 05:51
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]];
查看更多
欢心
4楼-- · 2020-01-26 05:51

In Swift:

let recipients = "someone@gmail.com"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)
查看更多
Summer. ? 凉城
5楼-- · 2020-01-26 05:52

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)
查看更多
Lonely孤独者°
6楼-- · 2020-01-26 05:53

If you are using Xamarin to developer an iOS application, here is the C# equivalent to open the mail application composer view:

string email = "yourname@companyname.com";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
查看更多
成全新的幸福
7楼-- · 2020-01-26 05:55

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