此代码可以正常工作,并显示所有已填写的电子邮件表格,并且可以很好地发送电子邮件,只是在发送或尝试取消后无法将其关闭。
我确定我缺少一些简单的东西。 我还意识到,如果有错误,我什么都没有,但是现在可以正常工作并发送电子邮件,但是我无法关闭它
尝试了一些我在网上找到的建议,还有其他方法可以做到这一点,但是我试图理解为什么它不起作用,所以希望这种方法(或关闭方法)起作用。...xcode 10.2.1
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func sendEmail(_ sender: Any) {
sendEmail()
}
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["xxx@xx.com"])
mail.setSubject("test ")
mail.setPreferredSendingEmailAddress("xxx@jxxx.com")
present(mail, animated: true)
} else {
//show failure alert
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
可以正常编译并发送电子邮件,只是无法关闭控制器
答案 0 :(得分:0)
所有委托方法都应在类范围内,在当前代码didFinishWith
中,该代码应将邮件嵌套在sendEmail
内
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func sendEmail(_ sender: Any) {
sendEmail()
}
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["xxx@xx.com"])
mail.setSubject("test ")
mail.setPreferredSendingEmailAddress("xxx@jxxx.com")
present(mail, animated: true)
} else {
//show failure alert
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}