Search code examples
iosswiftuitableviewmfmailcomposeviewcontroller

Make two different buttons create two different email subjects


I'm trying to get the two different buttons I have shown with if indexPath.section == 1 && indexPath.row == 0 and if indexPath.section == 1 && indexPath.row == 1 to have different subjects when i pull up the email. Both buttons pull up the same subject (Suggestion: ). How would I allow the program to differentiate them?

import UIKit
import Foundation
import MessageUI

class AccountViewController: UITableViewController, MFMailComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.view.backgroundColor = .clear

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 && indexPath.row == 1{
            print("pressed")
            let mailComposeViewController2 = configureMailController()
            if MFMailComposeViewController.canSendMail() {
                self.present(mailComposeViewController2, animated: true, completion: nil)
            } else {
                showMailError()
            }
        }
        if indexPath.section == 1 && indexPath.row == 0 {
            print("pressed")
            let mailComposeViewController = configureMailController()
            if MFMailComposeViewController.canSendMail() {
                self.present(mailComposeViewController, animated: true, completion: nil)
            } else {
                showMailError()
            }
        }
    }
    //if indexPath.section == 1 && indexPath.row == 1 {

    //}
        func configureMailController() -> MFMailComposeViewController {
            let mailComposerVC = MFMailComposeViewController()
            mailComposerVC.mailComposeDelegate = self
            mailComposerVC.setToRecipients(["[email protected]"])
            mailComposerVC.setSubject("Suggestion: ")

            return mailComposerVC
        }
        func configureMailController2() -> MFMailComposeViewController {
            let mailComposerVC = MFMailComposeViewController()
            mailComposerVC.mailComposeDelegate = self
            mailComposerVC.setToRecipients(["[email protected]"])
            mailComposerVC.setSubject("Report: ")

            return mailComposerVC
        }
        func showMailError() {
            let sendMailErrorAlert = UIAlertController(title: "Could not send email", message: "Your device could not send email", preferredStyle: .alert)
            let dismiss = UIAlertAction(title: "OK", style: .default, handler: nil)
            sendMailErrorAlert.addAction(dismiss)
            self.present(sendMailErrorAlert, animated: true, completion: nil)
        }
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith: MFMailComposeResult, error: Error?){
            controller.dismiss(animated: true, completion: nil)
        }

}

Solution

  • You need to add a parameter to help you:

    func configureMailController(subject = "Unspecified Subject") -> MFMailComposeViewController {
                let mailComposerVC = MFMailComposeViewController()
                mailComposerVC.mailComposeDelegate = self
                mailComposerVC.setToRecipients(["[email protected]"])
                mailComposerVC.setSubject(subject)
    
                return mailComposerVC
            }
    

    Then, do as you please when configuring:

    let mailComposeViewController1 = configureMailController("Specific Subject 1")
    

    ....

    let mailComposeViewController2 = configureMailController("Specific Subject 2")