MFMailComposeViewController拒绝解雇(MFMailComposeViewController refuse to dismiss)

编程入门 行业动态 更新时间:2024-10-16 20:25:54
MFMailComposeViewController拒绝解雇(MFMailComposeViewController refuse to dismiss)

这让我疯狂。 这段代码让用户发送一封电子邮件,其中包含应用内创建的图片。 除了self.dismiss(animated: true, completion: nil)之外,一切都很完美 - MFMailComposeViewController不会self.dismiss(animated: true, completion: nil) 。

我用这三个可能的问题https://stackoverflow.com/a/13217443/5274566作为我开始解决问题,但它仍然无法正常工作。 尽管事实上控制器保持不变,但邮件已发送或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

This is driving me nuts. This snippet of code lets the user send an email with an image which is created within the app. Everything works perfectly except the self.dismiss(animated: true, completion: nil) - the MFMailComposeViewController won't dismiss.

I used these three possibly problems https://stackoverflow.com/a/13217443/5274566 as my start to solve the problem, but it still won't work. The controller stays despite the fact than an mail has been sent or cancel has been tapped.

The protocol implementation MFMailComposeViewControllerDelegate is added.

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: delegate方法,所以它永远不会被调用,并且解雇代码将不会被执行。

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) }

Issue is you have written the didFinishWithResult: delegate method inside the mailOpen function, so it will never be called and the dismissing code won't be executed ever.

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) }

更多推荐

本文发布于:2023-08-01 01:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MFMailComposeViewController   refuse   dismiss

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!