【发布时间】:2012-02-07 23:11:11
【问题描述】:
我已经找到的是
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
但我只想打开邮件应用程序,而不仅仅是一个作曲家视图。只是处于正常或最后状态的邮件应用程序。
有什么想法吗?
【问题讨论】:
-
你用 imap 或 pop 代替 mailto 了吗?
我已经找到的是
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
但我只想打开邮件应用程序,而不仅仅是一个作曲家视图。只是处于正常或最后状态的邮件应用程序。
有什么想法吗?
【问题讨论】:
显然,邮件应用程序支持第二个 url 方案 - message://(我想)允许在应用程序获取特定消息时打开它。如果您不提供消息 url,它只会打开邮件应用程序:
NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
[[UIApplication sharedApplication] openURL:mailURL];
}
【讨论】:
message://http://domain/file.msg.
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]];
【讨论】:
stringByAddingPercentEscapesUsingEncoding 正在编码所有特殊字符(包括mailto 之后的冒号)。解决的办法是要么找到更好的编码函数,要么去掉编码行,通过将subject和body中的空格替换为%20来手动对消息进行编码。
原始 Amit 答案的 Swift 版本:
斯威夫特 5
func openEmailApp(toEmail: String, subject: String, body: String) {
guard
let subject = subject.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
else {
print("Error: Can't encode subject or body.")
return
}
let urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)"
let url = URL(string:urlString)!
UIApplication.shared.open(url)
}
Swift 3.0:
func openMailApp() {
let toEmail = "email@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)",
let url = URL(string:urlString)
{
UIApplication.shared().openURL(url)
}
}
斯威夫特 2:
func openMailApp() {
let toEmail = "email@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)
}
}
【讨论】:
urlString 应该是let urlString = "mailto:\(toEmail)?subject=\(subject.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)&body=\(body.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)"。这段代码可以重构为更好的格式和更安全的代码。
您可以通过使用 url 方案message:// 来打开邮件应用程序而不使用打开撰写视图
【讨论】:
由于启动其他应用程序的唯一方法是使用它们的 URL 方案,因此打开邮件的唯一方法是使用 mailto: 方案。不幸的是,对于您的情况,它将始终打开撰写视图。
【讨论】:
在真实设备上运行您的应用并调用
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"your@email.com"]];
注意,此行对模拟器无效。
【讨论】:
如果您知道其 URL 方案,您可以在 iOS 上启动任何应用程序。不知道邮件应用方案是公开的,但你可以偷偷摸摸地试试这个:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"message:message-id"]];
向 Farhad Noorzay 提供线索,让我了解这一点。这是对 Mail 应用程序 API 的一些逆向工程。更多信息在这里:https://medium.com/@vijayssundaram/how-to-deep-link-to-ios-7-mail-6c212bc79bd9
【讨论】:
扩展 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]];
【讨论】:
Swift 4 / 5 打开没有撰写视图的默认邮件应用程序。 如果 Mail 应用程序被删除,它会自动显示 UIAlert 以及重新下载应用程序的选项:)
UIApplication.shared.open(URL(string: "message:")!, options: [:], completionHandler: nil)
【讨论】:
Swift 5 版本:
if let mailURL = URL(string: "message:") {
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
}
【讨论】:
如果您使用 Xamarin 开发 iOS 应用程序,这里是打开邮件应用程序编写器视图的 C# 等效项:
string email = "yourname@companyname.com";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
【讨论】:
在 swift 2.3 上:打开邮箱
UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)
【讨论】:
它将使用 composer 视图打开 Default Mail App:
NSURL* mailURL = [NSURL URLWithString:@"mailto://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
[[UIApplication sharedApplication] openURL:mailURL];
}
它将打开默认邮件应用程序:
NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
[[UIApplication sharedApplication] openURL:mailURL];
}
【讨论】:
您可能想要使用脚本桥。我在我的应用程序中使用此方法直接为用户提供使用内置Mail.app 发送电子邮件通知的选项。我还构建了一个选项,可以直接通过 SMTP 执行此操作。
但由于您想使用Mail.app 方法,您可以通过以下方式找到有关如何执行该解决方案的更多信息:
https://github.com/HelmutJ/CocoaSampleCode/tree/master/SBSendEmail
【讨论】:
在斯威夫特中:
let recipients = "someone@gmail.com"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)
【讨论】: