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!