简体   繁体   中英

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?

Please try these code

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

override func viewDidLoad() {
    super.viewDidLoad()

    composeVC.mailComposeDelegate = self
    composeVC.setToRecipients(["desiredEmail@gmail.com"])
    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)
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM