0

I'm trying to implement a Email functionality inside my app, I wrote this class following a tutorial:

import UIKit
import MessageUI

class EmailController: UIViewController, MFMailComposeViewControllerDelegate {

@IBOutlet weak var subject: UITextField?
@IBOutlet weak var body: UITextView?
var alert: UIAlertView = UIAlertView()
var subjectText:String?
var bodyText:String?
var toSomeone:AnyObject!
var mailController:MFMailComposeViewController = MFMailComposeViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    //AlertView
    alert.title = "Error"
    alert.message = "Faltan datos"
    alert.addButtonWithTitle("Ok")
    //Recipients
    toSomeone = "[email protected]"
    //asignar delegado al controlador de email
    mailController.mailComposeDelegate = self


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}



@IBAction func sendEmail(sender: UIBarButtonItem) {

    //bool var becouse optionals can't be used like bools
    var condicion1:Bool? = subject?.text.isEmpty
    var condicion2:Bool? = body?.text.isEmpty

    //unwraped the variables
    if (!condicion1! && !condicion2!) {

        subjectText = self.subject!.text
        bodyText = self.body!.text

        //Completar objeto mailController

        mailController.setSubject(subjectText)
        mailController.setMessageBody(bodyText, isHTML: false)

        var recipients = [toSomeone]

        mailController.setToRecipients(recipients)

        self.presentViewController(mailController, animated: true, completion: nil)

    }else {

        self.alert.show()

    }




}


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


    switch result.value {

    case MFMailComposeResultCancelled.value:
        //se cancelo envio
        alert.title = "Envio cancelado"
        alert.message = "Se cancelo el envio"
        alert.addButtonWithTitle("Ok")
        alert.show()

    case MFMailComposeResultSaved.value:
        //se guardo draft
        alert.title = "Correo guardado"
        alert.message = "Se guardo el correo en la app de Mail"
        alert.addButtonWithTitle("Ok")
        alert.show()

    case MFMailComposeResultFailed.value:
        //fallo el envio
        alert.title = "Error"
        alert.message = "El correo no pudo ser enviado"
        alert.addButtonWithTitle("Ok")
        alert.show()

    case MFMailComposeResultSent.value:
        //el mail se pudo enviar y esta en la pila de envio
        alert.title = "Correo enviado"
        alert.message = "El correo se envio exitosamente"
        alert.addButtonWithTitle("Ok")
        alert.show()

    default:
        break


    }

                                                    //bloque de codigo a ejecutar al finalizar de mostrar la vista
    mailController.dismissViewControllerAnimated(true, nil)



     }

   }

I can send the email just fine but when I dismiss the mail view I see my initial view (thats ok) the problem is that if I try to invoke the email view again(I mean try to send another email) the program don't respond and I can't dismiss it anymore, I think that I need to reload the initial view???

Thanks for your help!

1 Answer 1

1

You can present a new MFMailComposeViewController each time you send an email, like this

mailController = MFMailComposeViewController()

Declare it like this.

var mailController:MFMailComposeViewController?
Sign up to request clarification or add additional context in comments.

4 Comments

Cool! your solution works! : D I just have one more question, How de-alocate my objects? if I am not wrong I can execute code after my mail view get dismiss with this method: mailController?.dismissViewControllerAnimated(true, nil), but I don't know how to put a block of code inside that method and I don't know how to set the memory free for my objects, I think it's something like this maybe? : object1 = nil . Thanks for your help!
If you declare var mailController:MFMailComposeViewController?, you can use mailController = .None or mailController = nil to set it to nil. More from here
ok :), but do you know how to put a block of code here (in the completion parameter)? self.presentViewController(mailController, animated: true, completion: nil) I tried with a clousure but I think I put it in the wrong way
Please try this:self.presentViewController(mailController!, animated: true, completion: { () -> Void in println(self.presentedViewController) })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.