maclacerda
maclacerda

Reputation: 3

MFMailComposeViewController back and send button not working

I tried using the MFMailComposeViewController to send an email in my app.

let email = "..."

let mailComposer = MFMailComposeViewController()

mailComposer.mailComposeDelegate = self                
mailComposer.setToRecipients([email])

self.navigationController?.present(mailComposer, animated: true)

After running my app the composer screen is showing, but the cancel and send buttons are not showing. I tried many possible solution, as change tintColor in both navigationControllers. For example:

mailComposer.navigationBar.tintColor = .red

But the "issue" persists.

Any ideas?

Upvotes: 0

Views: 1240

Answers (2)

Vitalii Gozhenko
Vitalii Gozhenko

Reputation: 9354

mailComposeDelegate should be inherited from UIViewController. In other case dismissing MFMailComposeViewController not working and crashes. Seems like Apple bug

Upvotes: 0

Hardik Bar
Hardik Bar

Reputation: 1760

Please try these code

class TechSupportVC: UIViewController, MFMailComposeViewControllerDelegate {
    let composeVC = MFMailComposeViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    composeVC.mailComposeDelegate = self
    composeVC.setToRecipients(["[email protected]"])
    composeVC.setSubject("My message")
}

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

@IBAction func sendPressed(_ sender: Any) {
    guard MFMailComposeViewController.canSendMail() else {
        showMailServiceErrorAlert()
        return
    }

    composeVC.setMessageBody("Test credentials: \(firstAndLastNameTextField.text!)\nPhone: \(numberTextField.text!)\n\n\(messageTextView.text!)", isHTML: false)

    self.present(composeVC, animated: true, completion: nil)
}

Upvotes: 1

Related Questions