这让我疯狂。 这段代码让用户发送一封电子邮件,其中包含应用内创建的图片。 除了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 mailThis 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) }更多推荐
发布评论