如何在 Swift 中启动 iOS 邮件应用程序?
How to launch the iOS mail app in Swift?
我正在创建一个 iOS 应用程序,它具有密码重置功能,可以向用户发送电子邮件。发送电子邮件后,我想向用户显示 UIAlertController
,询问他们是否愿意打开邮件应用程序。
我在这里看到了各种帖子:
let url = NSURL(string: "mailto:")
UIApplication.sharedApplication().openURL(url!)
这行得通,但不幸的是它启动了一条新消息,这不是我想要的。我只想启动应用程序,以便用户可以看到他们的收件箱。
我自己没有测试过,但也许这个答案会对你有所帮助:
Apparently Mail supports a second url scheme message://
which (I suppose) allows you to open a specific message if it was fetched by your application. If you do not provide a full message url, it will just open Mail:
let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.openURL(mailURL)
}
取自:Launch Apple Mail App from within my own App?
Swift 3.0.1 打开邮件应用程序的方法如下:
private func openMailClient() {
let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.openURL(mailURL)
}
}
如 "dehlen" 正确所述,如果未提供更多信息,使用 message://
方案只会打开邮件应用程序。
显然几年后......我不得不为 Xcode 10.2.1 swift 5.
添加一个完成处理程序
这很完美-
let emailURL = NSURL(string: "message://")!
if UIApplication.shared.canOpenURL(emailURL as URL)
{
UIApplication.shared.open(emailURL as URL, options: [:],completionHandler: nil)
}
由于 UIApplication.shared.openURL()
方法已被弃用,我们可以直接使用 URL()
代替 NSURL()
,此问题答案的更新版本是:
let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
这将适用于 Xcode 11.5:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 5:
openEmailApp(email: "email@gmail.com")
default:
break
}
}
func openEmailApp(email: String) {
if let url = URL(string: "mailto: \(email)") {
UIApplication.shared.open(url)
我正在创建一个 iOS 应用程序,它具有密码重置功能,可以向用户发送电子邮件。发送电子邮件后,我想向用户显示 UIAlertController
,询问他们是否愿意打开邮件应用程序。
我在这里看到了各种帖子:
let url = NSURL(string: "mailto:")
UIApplication.sharedApplication().openURL(url!)
这行得通,但不幸的是它启动了一条新消息,这不是我想要的。我只想启动应用程序,以便用户可以看到他们的收件箱。
我自己没有测试过,但也许这个答案会对你有所帮助:
Apparently Mail supports a second url scheme
message://
which (I suppose) allows you to open a specific message if it was fetched by your application. If you do not provide a full message url, it will just open Mail:
let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.openURL(mailURL)
}
取自:Launch Apple Mail App from within my own App?
Swift 3.0.1 打开邮件应用程序的方法如下:
private func openMailClient() {
let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.openURL(mailURL)
}
}
如 "dehlen" 正确所述,如果未提供更多信息,使用 message://
方案只会打开邮件应用程序。
显然几年后......我不得不为 Xcode 10.2.1 swift 5.
添加一个完成处理程序这很完美-
let emailURL = NSURL(string: "message://")!
if UIApplication.shared.canOpenURL(emailURL as URL)
{
UIApplication.shared.open(emailURL as URL, options: [:],completionHandler: nil)
}
由于 UIApplication.shared.openURL()
方法已被弃用,我们可以直接使用 URL()
代替 NSURL()
,此问题答案的更新版本是:
let mailURL = URL(string: "message://")!
if UIApplication.shared.canOpenURL(mailURL) {
UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
}
这将适用于 Xcode 11.5:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 5:
openEmailApp(email: "email@gmail.com")
default:
break
}
}
func openEmailApp(email: String) {
if let url = URL(string: "mailto: \(email)") {
UIApplication.shared.open(url)