Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't close mail view controller when clicking Cancel, Save Draft or Delete Draft

Tags:

email

xcode

swift

I'm implementing a mail controller in my app but I can't figure out why it won't close when clicking save draft or delete draft. The window gets stuck on the email screen and I can't click "Cancel" a second time either.

@IBAction func emailButtonTapped(_ sender: UIButton) {
    guard MFMailComposeViewController.canSendMail() else {
        if !MFMailComposeViewController.canSendMail() {
            print("Can not send email")
            return
        }
        return
    }


    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        dismiss(animated: true, completion: nil)
    }

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["[email protected]"])
    mailComposer.setSubject("Look at this")
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)

    present(mailComposer, animated: true, completion: nil)
}
like image 649
Wizzardzz Avatar asked Jan 29 '23 02:01

Wizzardzz


2 Answers

I successfully fixed the issue, and it was a pretty stupid one! Some code was outside the emailButtonTapped func:

@IBAction func emailButtonTapped(_ sender: UIButton) {
    //guard MFMailComposeViewController.canSendMail() else {
        if !MFMailComposeViewController.canSendMail() {
            print("Can not send email")
            return
        }
    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    mailComposer.setToRecipients(["[email protected]"])
    mailComposer.setSubject("Look at this")
    mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)

    present(mailComposer, animated: true, completion: nil)
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    dismiss(animated: true, completion: nil)
}
like image 78
Wizzardzz Avatar answered Jan 31 '23 15:01

Wizzardzz


Swift 4

// MARK: - SEND EMAIL BUTTON

@IBAction func SendEmailButt(_ sender: AnyObject) {
    // This string containes standard HTML tags, you can edit them as you wish

    let messageStr = "Hello,"

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self
    mailComposer.setSubject("Title")
    mailComposer.setMessageBody(messageStr, isHTML: true)

    if MFMailComposeViewController.canSendMail() { present(mailComposer, animated: true, completion: nil)
    } else {
        print("Your device cannot send emails. Please configure an email address into Settings -> Mail, Contacts, Calendars.")
    }
}




 // Email delegate

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        var resultMess = ""
        switch result.rawValue {
        case MFMailComposeResult.cancelled.rawValue:
            resultMess = "Mail cancelled"
        case MFMailComposeResult.saved.rawValue:
            resultMess = "Mail saved"
        case MFMailComposeResult.sent.rawValue:
            resultMess = "Thanks for contacting us!\nWe'll get back to you asap."
        case MFMailComposeResult.failed.rawValue:
            resultMess = "Something went wrong with sending Mail, try again later."
        default:break
        }

        // Show email result alert
        print(resultMess)
        dismiss(animated: true, completion: nil)
}
like image 37
Faris Avatar answered Jan 31 '23 14:01

Faris