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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8821934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:14:49  来源:igfitidea点击:

Launch Apple Mail App from within my own App?

iosemail

提问by cschuff

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?

有任何想法吗?

回答by Vladimir

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:

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

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

回答by Amit

NSString *recipients = @"mailto:[email protected][email protected],[email protected]&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]];

回答by VojtaStavik

Swift version of the original Amit's answer:

原始 Amit 答案的 Swift 版本:

Swift 2:

斯威夫特 2:

func openMailApp() {

    let toEmail = "[email protected]"
    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:

斯威夫特 3.0:

func openMailApp() {

    let toEmail = "[email protected]"
    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)
    }
}

回答by Briggs

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.

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

回答by Jesse S.

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

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

回答by catanore

Run your app on a real device and call

在真实设备上运行您的应用程序并调用

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"[email protected]"]];

Note, that this line takes no effect on simulator.

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

回答by Diogenes

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:

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

[[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

Farhad Noorzay 为我提供线索的道具。这是对邮件应用程序 API 的一些逆向工程。更多信息:https: //medium.com/@vijayssundaram/how-to-deep-link-to-ios-7-mail-6c212bc79bd9

回答by Stan Tatarnykov

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.

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

//put email info here:
NSString *toEmail=@"[email protected]";
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]];

回答by Michael Kniskern

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

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

string email = "[email protected]";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);

回答by Pacyjent

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

Swift 4 / 5 无需撰写视图即可打开默认邮件应用程序。如果邮件应用程序被删除,它会自动显示 UIAlert 和重新下载应用程序的选项:)

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