UIButton does not bring up MFMailViewController

64 Views Asked by At

I'm having an error with a bit of code. I am new to this and I am trying to teach myself. I am finding most of my answers online but can't seem to find anything about this particular issue. I want to send an email within the app but anytime I press the email button, the MFMailViewController does not come up. It is like my UIButton isn't working. But I know I have it as an IBAction. Here is my code so far. Any help is much appreciated.

import UIKit
import MessageUI

class RequestService: UIViewController,MFMailComposeViewControllerDelegate {
    @IBOutlet weak var CustomerName: UITextField!
    @IBOutlet weak var emailButton: UIButton!

    @IBAction func sendEmail(_ sender: UIButton) {
        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")

            let ComposeVC = MFMailComposeViewController()
            ComposeVC.mailComposeDelegate = self

            ComposeVC.setToRecipients(["[email protected]"])
            ComposeVC.setSubject("New Support Ticket")
            ComposeVC.setMessageBody(CustomerName.text!, isHTML: false)

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

        func mailComposeController(controller: MFMailComposeViewController,didFinishWithResult result:MFMailComposeResult, error: NSError?) {
            // Check the result or perform other tasks.
            // Dismiss the mail compose view controller.
            controller.dismiss(animated: true, completion: nil)
        }
    }
}
2

There are 2 best solutions below

2
Colin Basnett On BEST ANSWER

You made an error in the syntax in your sendMail function. The code you posted will only open the view controller if the device can't send mail. Change it to this:

@IBAction func sendEmail(_ sender: UIButton) {
    if !MFMailComposeViewController.canSendMail() {
        print("Mail services are not available")
        return
    }

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self
    composeVC.setToRecipients(["[email protected]"])
    composeVC.setSubject("New Support Ticket")
    composeVC.setMessageBody(CustomerName.text!, isHTML: false)

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

You only attempt to display the mail controller if the device can't send email. That's backwards.

@IBAction func sendEmail(_ sender: UIButton) {
    if MFMailComposeViewController.canSendMail() {
        print("Mail services are not available")

        let ComposeVC = MFMailComposeViewController()
        ComposeVC.mailComposeDelegate = self

        ComposeVC.setToRecipients(["[email protected]"])
        ComposeVC.setSubject("New Support Ticket")
        ComposeVC.setMessageBody(CustomerName.text!, isHTML: false)

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

func mailComposeController(controller: MFMailComposeViewController,didFinishWithResult result:MFMailComposeResult, error: NSError?) {
    // Check the result or perform other tasks.
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}

And you need the delegate method outside of the other method.