MFMailComposeViewController 拒绝关闭
MFMailComposeViewController refuse to dismiss
这让我抓狂。这段代码允许用户发送一封电子邮件,其中包含在应用程序中创建的图像。除了 self.dismiss(animated: true, completion: nil)
- MFMailComposeViewController
不会关闭。
一切正常
我用这三个可能的问题作为解决问题的起点,但还是不行。尽管已发送邮件或已点击 cancel
,但控制器仍然存在。
添加了协议实现MFMailComposeViewControllerDelegate
。
func mailOpen(alertAction: UIAlertAction) {
if MFMailComposeViewController.canSendMail() {
let mailcontroller = MFMailComposeViewController()
mailcontroller.mailComposeDelegate = self;
mailcontroller.setSubject("Subject")
let completeImage = newImage! as UIImage
mailcontroller.addAttachmentData(UIImageJPEGRepresentation(completeImage, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "Image")
mailcontroller.setMessageBody("<html><body><p>Message</p></body></html>", isHTML: true)
self.present(mailcontroller, animated: true, completion: nil)
} else {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send the e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "Got it!")
sendMailErrorAlert.show()
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismiss(animated: true, completion: nil)
}
}//end of mail
问题是您在 mailOpen
函数中编写了 didFinishWithResult:
委托方法,因此永远不会调用它,也不会执行关闭代码。
func mailOpen(alertAction: UIAlertAction)
{
if MFMailComposeViewController.canSendMail()
{
let mailcontroller = MFMailComposeViewController()
mailcontroller.mailComposeDelegate = self;
mailcontroller.setSubject("Subject")
let completeImage = newImage! as UIImage
mailcontroller.addAttachmentData(UIImageJPEGRepresentation(completeImage, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "Image")
mailcontroller.setMessageBody("<html><body><p>Message</p></body></html>", isHTML: true)
self.present(mailcontroller, animated: true, completion: nil)
}
else
{
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send the e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "Got it!")
sendMailErrorAlert.show()
}
}//end of mail
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?)
{
self.dismiss(animated: true, completion: nil)
}
此处:
self.dismiss(animated: true, completion: nil)
您正在解雇自己的 ViewController,而不是 MFMailComposeViewController
应该是:
controller.dismiss(animated: true, completion: nil)
这让我抓狂。这段代码允许用户发送一封电子邮件,其中包含在应用程序中创建的图像。除了 self.dismiss(animated: true, completion: nil)
- MFMailComposeViewController
不会关闭。
我用这三个可能的问题作为解决问题的起点,但还是不行。尽管已发送邮件或已点击 cancel
,但控制器仍然存在。
添加了协议实现MFMailComposeViewControllerDelegate
。
func mailOpen(alertAction: UIAlertAction) {
if MFMailComposeViewController.canSendMail() {
let mailcontroller = MFMailComposeViewController()
mailcontroller.mailComposeDelegate = self;
mailcontroller.setSubject("Subject")
let completeImage = newImage! as UIImage
mailcontroller.addAttachmentData(UIImageJPEGRepresentation(completeImage, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "Image")
mailcontroller.setMessageBody("<html><body><p>Message</p></body></html>", isHTML: true)
self.present(mailcontroller, animated: true, completion: nil)
} else {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send the e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "Got it!")
sendMailErrorAlert.show()
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismiss(animated: true, completion: nil)
}
}//end of mail
问题是您在 mailOpen
函数中编写了 didFinishWithResult:
委托方法,因此永远不会调用它,也不会执行关闭代码。
func mailOpen(alertAction: UIAlertAction)
{
if MFMailComposeViewController.canSendMail()
{
let mailcontroller = MFMailComposeViewController()
mailcontroller.mailComposeDelegate = self;
mailcontroller.setSubject("Subject")
let completeImage = newImage! as UIImage
mailcontroller.addAttachmentData(UIImageJPEGRepresentation(completeImage, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "Image")
mailcontroller.setMessageBody("<html><body><p>Message</p></body></html>", isHTML: true)
self.present(mailcontroller, animated: true, completion: nil)
}
else
{
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send the e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "Got it!")
sendMailErrorAlert.show()
}
}//end of mail
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?)
{
self.dismiss(animated: true, completion: nil)
}
此处:
self.dismiss(animated: true, completion: nil)
您正在解雇自己的 ViewController,而不是 MFMailComposeViewController
应该是:
controller.dismiss(animated: true, completion: nil)