您好,我想从我的应用程序打开电子邮件程序,并且正文应该已经定义。我可以打开电子邮件,但不知道如何将电子邮件正文定义为给定参数以显示给定标准文本。有人可以帮忙吗?这是我用来打开电子邮件的代码:
//EMAIL
let email = "[email protected]"
let urlEMail = NSURL(string: "mailto:\(email)")
if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}
如果您想打开内置电子邮件应用程序而不是像其他人提到的那样显示
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:[email protected]?\(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:[email protected]?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:[email protected]?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)
}
}
您可以使用
MFMailComposeViewController
来做到这一点:
import MessageUI
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["[email protected]"])
mailComposerVC.setSubject("Subject")
mailComposerVC.setMessageBody("Body", isHTML: false)
self.presentViewController(mailComposerVC, animated: true, completion: nil)
此外,您需要从
mailComposeController:didFinishWithResult:error:
实现 MFMailComposeViewControllerDelegate
,您应该在其中忽略 MFMailComposeViewController
斯威夫特4.0
let email = "[email protected]"
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.")
}
})
}
}
}
像这样使用
MFMailComposeViewController
:
导入MessageUI
import MessageUI
将代表添加到您的班级:
class myClass: UIViewController, MFMailComposeViewControllerDelegate {}
配置您想要的电子邮件预设
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("Subject")
mail.setMessageBody("Body", isHTML: true)
mail.setToRecipients(["[email protected]"])
presentViewController(mail, animated: true, completion: nil)
将此方法放入您的代码中:
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
dismissViewControllerAnimated(true, completion: nil)
}
好了,现在可以工作了。
更新至 Xcode 12.5
let url = NSURL(string: "mailto:mailto:[email protected]")
UIApplication.shared.open(url! as URL)
或者如果您想添加嵌入主题
let url = NSURL(string: "mailto:[email protected]?subject=This%20is%20the%20subject&[email protected]&body=This%20is%20the%20body")
或者如果您想添加多个电子邮件地址
let url = NSURL(string: "mailto:mailto:[email protected],[email protected]")
UIApplication.shared.open(url! as URL)
与其他答案中的 url 构造类似,但您可以使用
addingPercentEncoding
:而不是调用
URLComponents
var components = URLComponents(string: "[email protected]")
components?.queryItems = [URLQueryItem(name: "subject", value: "Your Subject")]
if let mailUrl = components?.url {
UIApplication.shared.open(mailUrl, options: [:], completionHandler: nil)
}
@mclaughj 答案的 Swift 5 版本
let email = "[email protected]"
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)
}
}
干杯!
我建议使用苹果的方式,你可以在MFMailComposeViewController的官方文档中找到。它会打开一个带有电子邮件的模态视图控制器,该电子邮件在发送后会被关闭。因此,用户会留在您的应用程序中。
class EmailUrl {
static func getFrom(email: String, subject: String = "", body: String = "") -> URL {
if let encodedParams = "subject=\(subject)&body=\(body)"
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
return URL(string: "mailto:\(email)?\(encodedParams)")!
}
return URL(string: "mailto:\(email)")!
}
}
iOS 和 macOS 中 SwiftUI 的使用:
Link("Support Email", destination: EmailUrl.getFrom(email: "[email protected]", subject: "VideQ feedback") )