0

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
Community
  • 1
  • 1
Mate
  • 99
  • 10

2 Answers2

2

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)
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Opening brace should not be wrapped (see https://github.com/raywenderlich/swift-style-guide#spacing). – Andrii Chernenko Dec 21 '16 at 20:26
  • 2
    @PEEJWEEJ There's no reason to edit someone's answer just to change the placement of curly braces. Different people prefer different styles. – rmaddy Dec 21 '16 at 21:38
  • @deville: There is no official document saying that which style should be followed. I prefer this style as it is very easier to me for matching opening and closing brace. – Midhun MP Dec 23 '16 at 21:01
  • Thank you! Silly mistake – Mate Dec 31 '16 at 12:14
-3

here:

self.dismiss(animated: true, completion: nil)

you're dismissing your own ViewController, rather than the MFMailComposeViewController

It should be:

controller.dismiss(animated: true, completion: nil)
GetSwifty
  • 7,568
  • 1
  • 29
  • 46