您好我想从我的应用程序打开电子邮件程序,并且已经定义了正文。我可以打开电子邮件但不知道如何将电子邮件正文定义为给定参数以显示给定的标准文本。有人可以帮忙吗?下面是用于打开电子邮件的代码:
//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:\(email)")
if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}
答案 0 :(得分:21)
如果您要打开内置电子邮件应用,而不是像其他人一样提及MFMailComposeViewController
,则可以构建一个mailto:
链接,如下所示:
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)
}
答案 1 :(得分:15)
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)
}
}
答案 2 :(得分:14)
您可以使用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)
此外,您需要实施mailComposeController:didFinishWithResult:error:
MFMailComposeViewControllerDelegate
,您应该解除MFMailComposeViewController
答案 3 :(得分:6)
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.")
}
})
}
}
}
答案 4 :(得分:4)
像这样使用MFMailComposeViewController
:
导入MessageUI
import MessageUI
将代表添加到您的班级:
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)
}
你去,现在工作。
答案 5 :(得分:0)
我建议使用Apple的方式,您可以在官方文档中找到MFMailComposeViewController。它打开一个带有电子邮件的模态视图控制器,在发送后被解除。因此,用户会留在您的应用中。
答案 6 :(得分:0)
与其他答案中的url构建类似,但是可以使用addingPercentEncoding
来代替调用URLComponents
:
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)
}
答案 7 :(得分:0)
@mclaughj 答案的 Swift 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)
}
}
干杯!