Search code examples
iosemailswift2tableviewmessageui

Swift 2: Email Cancel Button Stops Working After Second Try on a Table View


I'm using a table view that has a cell that says "Send Us Feedback." It opens up the email application with preset information to be sent. I can send the email and/or cancel it, but when I cancel it the first time, stay on the table view, tap the cell again to open the email, I can't cancel it again. It just stays on the email view.

Suggestions?

My code is below:

import UIKit
import MessageUI

class FeedbackViewController: UITableViewController, MFMailComposeViewControllerDelegate {

    let mailComposerVC = MFMailComposeViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if indexPath.section == 0 && indexPath.row == 0
        {
            let alertController = UIAlertController(title: "Rate Us", message: "\nAre you enjoying our app? Please rate us in the app store!\n\nElse if you know of ways we can make our app better, please send us feedback so we can improve the experience for you!\n\nThank you!\n\nTimmy Caish", preferredStyle: .Alert)

            alertController.addAction(UIAlertAction(title: "Rate on iTunes", style: .Default, handler: {
                (action: UIAlertAction!) -> Void in
                UIApplication.sharedApplication().openURL((NSURL(string: "http://google.com")!))
                print("Rate us alert button worked.")
                print("Send to iTunes")
            }))

            alertController.addAction(UIAlertAction(title: "Send Us Feedback", style: .Default, handler: {
                (action: UIAlertAction!) in
                print("Rate Us feeback button worked.")
                let mailComposeViewController = self.configureMailComposeViewController()
                if MFMailComposeViewController.canSendMail()
                {
                    self.presentViewController(mailComposeViewController, animated: true, completion: nil)
                }
                else
                {
                    self.showSendMailErrorAlert()
                }
            }))

            alertController.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction) in
                print("Rate us cancel button worked.")
            print("Rate us")
            }))

            presentViewController(alertController, animated: true, completion: nil)
        }


        if indexPath.section == 0 && indexPath.row == 1
        {
            let mailComposeViewController = configureMailComposeViewController()

            if MFMailComposeViewController.canSendMail()
            {
                self.presentViewController(mailComposeViewController, animated: true, completion: nil)
            }
            else
            {
                self.showSendMailErrorAlert()
            }

            print("Send us feedback")
        }

    }

    func configureMailComposeViewController() -> MFMailComposeViewController {

        mailComposerVC.mailComposeDelegate = self

        mailComposerVC.setToRecipients(["[email protected]"])
        mailComposerVC.setSubject("Weather Simplicity Feedback")
        mailComposerVC.setMessageBody("Hello,\n\nI would like to share the following feedback...\n\n", isHTML: false)

        return mailComposerVC

    }

    func showSendMailErrorAlert() {

        let sendMailErrorAlert = UIAlertController(title: "Error", message: "Your device could not send the email. Check your email configurations and try again.", preferredStyle: UIAlertControllerStyle.Alert)
        let okay = UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil)
        sendMailErrorAlert.addAction(okay)
        self.presentViewController(sendMailErrorAlert, animated: true, completion: nil)
    }

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

        switch result {

        case MFMailComposeResultCancelled:
            print("Cancelled mail")
            break
        case MFMailComposeResultSent:
            print("Message sent")
            break
        default:
            break
        }

        self.dismissViewControllerAnimated(true, completion: nil)

    }

}

Solution

  • Try creating a new mailViewController only when you need it in didSelectRow rather than at the creation of the view controller. This will ensure it is clean each time and is not created unless needed and may fix the problem of the second cancel not working.