使用 iOS 中的预定义文本从 App 打开电子邮件
Open eMail from App with predefined text in iOS
你好,我想从我的应用程序打开电子邮件程序,正文应该已经定义好了。我可以打开电子邮件,但不知道如何将电子邮件正文定义为给定参数以显示给定的标准文本。任何人都可以帮忙吗?这是我用来打开电子邮件的代码:
//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:\(email)")
if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}
您可以使用 MFMailComposeViewController
:
import MessageUI
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["email@email.com"])
mailComposerVC.setSubject("Subject")
mailComposerVC.setMessageBody("Body", isHTML: false)
self.presentViewController(mailComposerVC, animated: true, completion: nil)
此外,您需要从 MFMailComposeViewControllerDelegate
实施 mailComposeController:didFinishWithResult:error:
,您应该在其中关闭 MFMailComposeViewController
像这样使用MFMailComposeViewController
:
导入 MessageUI
import MessageUI
将代表添加到您的 class:
class myClass: UIViewController, MFMailComposeViewControllerDelegate {}
配置您想要的电子邮件预设
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("Subject")
mail.setMessageBody("Body", isHTML: true)
mail.setToRecipients(["my@email.com"])
presentViewController(mail, animated: true, completion: nil)
将此方法放入您的代码中:
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
dismissViewControllerAnimated(true, completion: nil)
}
好了,现在开始工作了。
我建议使用Apple的方式,你可以在MFMailComposeViewController的官方文档中找到。它打开一个带有电子邮件的模态视图控制器,发送后关闭。因此用户会留在您的应用中。
如果您想打开内置的电子邮件应用程序而不是像其他人提到的那样显示 MFMailComposeViewController
,您可以构建一个 mailto:
link 之类的这个:
let subject = "My subject"
let body = "The awesome body of my email."
let encodedParams = "subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let url = "mailto:foo@bar.com?\(encodedParams)"
if let emailURL = NSURL(url) {
if UIApplication.sharedApplication().canOpenURL(emailURL) {
UIApplication.sharedApplication().openURL(emailURL)
}
}
为了节省任何人的打字时间,2016 年的语法略有变化:
let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah@blah.com?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.sharedApplication().canOpenURL(emailURL)
{
UIApplication.sharedApplication().openURL(emailURL)
}
Swift 3 版本
let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah@blah.com?subject=\(subject)&body=\(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL: NSURL = NSURL(string: coded!) {
if UIApplication.shared.canOpenURL(emailURL as URL) {
UIApplication.shared.openURL(emailURL as URL)
}
}
Swift 4.0
let email = "feedback@company.com"
let subject = "subject"
let bodyText = "Please provide information that will help us to serve you better"
if MFMailComposeViewController.canSendMail() {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients([email])
mailComposerVC.setSubject(subject)
mailComposerVC.setMessageBody(bodyText, isHTML: true)
self.present(mailComposerVC, animated: true, completion: nil)
} else {
let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL = URL(string: coded!)
{
if UIApplication.shared.canOpenURL(emailURL)
{
UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
if !result {
// show some Toast or error alert
//("Your device is not currently configured to send mail.")
}
})
}
}
}
类似于其他答案中的 url 构造,但您可以使用 URLComponents
:
而不是调用 addingPercentEncoding
var components = URLComponents(string: "youremail@test.com")
components?.queryItems = [URLQueryItem(name: "subject", value: "Your Subject")]
if let mailUrl = components?.url {
UIApplication.shared.open(mailUrl, options: [:], completionHandler: nil)
}
Swift @mclaughj 回答的第 5 个版本
let email = "foo@bar.com"
let subject = "Your Subject"
let body = "Plenty of email body."
let coded = "mailto:\(email)?subject=\(subject)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.shared.canOpenURL(emailURL as URL){
UIApplication.shared.open(emailURL as URL)
}
}
干杯!
更新为 Xcode 12.5
let url = NSURL(string: "mailto:mailto:someone@example.com")
UIApplication.shared.open(url! as URL)
或者如果您想添加嵌入式主题
let url = NSURL(string: "mailto:someone@example.com?subject=This%20is%20the%20subject&cc=someone_else@example.com&body=This%20is%20the%20body")
或者如果您想添加多个电子邮件地址
let url = NSURL(string: "mailto:mailto:someone@example.com,someoneelse@example.com")
UIApplication.shared.open(url! as URL)
你好,我想从我的应用程序打开电子邮件程序,正文应该已经定义好了。我可以打开电子邮件,但不知道如何将电子邮件正文定义为给定参数以显示给定的标准文本。任何人都可以帮忙吗?这是我用来打开电子邮件的代码:
//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:\(email)")
if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}
您可以使用 MFMailComposeViewController
:
import MessageUI
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["email@email.com"])
mailComposerVC.setSubject("Subject")
mailComposerVC.setMessageBody("Body", isHTML: false)
self.presentViewController(mailComposerVC, animated: true, completion: nil)
此外,您需要从 MFMailComposeViewControllerDelegate
实施 mailComposeController:didFinishWithResult:error:
,您应该在其中关闭 MFMailComposeViewController
像这样使用MFMailComposeViewController
:
导入 MessageUI
import MessageUI
将代表添加到您的 class:
class myClass: UIViewController, MFMailComposeViewControllerDelegate {}
配置您想要的电子邮件预设
let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setSubject("Subject") mail.setMessageBody("Body", isHTML: true) mail.setToRecipients(["my@email.com"]) presentViewController(mail, animated: true, completion: nil)
将此方法放入您的代码中:
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { dismissViewControllerAnimated(true, completion: nil) }
好了,现在开始工作了。
我建议使用Apple的方式,你可以在MFMailComposeViewController的官方文档中找到。它打开一个带有电子邮件的模态视图控制器,发送后关闭。因此用户会留在您的应用中。
如果您想打开内置的电子邮件应用程序而不是像其他人提到的那样显示 MFMailComposeViewController
,您可以构建一个 mailto:
link 之类的这个:
let subject = "My subject"
let body = "The awesome body of my email."
let encodedParams = "subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let url = "mailto:foo@bar.com?\(encodedParams)"
if let emailURL = NSURL(url) {
if UIApplication.sharedApplication().canOpenURL(emailURL) {
UIApplication.sharedApplication().openURL(emailURL)
}
}
为了节省任何人的打字时间,2016 年的语法略有变化:
let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah@blah.com?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.sharedApplication().canOpenURL(emailURL)
{
UIApplication.sharedApplication().openURL(emailURL)
}
Swift 3 版本
let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah@blah.com?subject=\(subject)&body=\(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL: NSURL = NSURL(string: coded!) {
if UIApplication.shared.canOpenURL(emailURL as URL) {
UIApplication.shared.openURL(emailURL as URL)
}
}
Swift 4.0
let email = "feedback@company.com"
let subject = "subject"
let bodyText = "Please provide information that will help us to serve you better"
if MFMailComposeViewController.canSendMail() {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients([email])
mailComposerVC.setSubject(subject)
mailComposerVC.setMessageBody(bodyText, isHTML: true)
self.present(mailComposerVC, animated: true, completion: nil)
} else {
let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL = URL(string: coded!)
{
if UIApplication.shared.canOpenURL(emailURL)
{
UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
if !result {
// show some Toast or error alert
//("Your device is not currently configured to send mail.")
}
})
}
}
}
类似于其他答案中的 url 构造,但您可以使用 URLComponents
:
addingPercentEncoding
var components = URLComponents(string: "youremail@test.com")
components?.queryItems = [URLQueryItem(name: "subject", value: "Your Subject")]
if let mailUrl = components?.url {
UIApplication.shared.open(mailUrl, options: [:], completionHandler: nil)
}
Swift @mclaughj 回答的第 5 个版本
let email = "foo@bar.com"
let subject = "Your Subject"
let body = "Plenty of email body."
let coded = "mailto:\(email)?subject=\(subject)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.shared.canOpenURL(emailURL as URL){
UIApplication.shared.open(emailURL as URL)
}
}
干杯!
更新为 Xcode 12.5
let url = NSURL(string: "mailto:mailto:someone@example.com")
UIApplication.shared.open(url! as URL)
或者如果您想添加嵌入式主题
let url = NSURL(string: "mailto:someone@example.com?subject=This%20is%20the%20subject&cc=someone_else@example.com&body=This%20is%20the%20body")
或者如果您想添加多个电子邮件地址
let url = NSURL(string: "mailto:mailto:someone@example.com,someoneelse@example.com")
UIApplication.shared.open(url! as URL)