【发布时间】:2014-12-10 03:23:09
【问题描述】:
我正在尝试在我的应用程序中实现电子邮件功能,我按照教程编写了这个类:
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 = "xxxxxx@gmail.com"
//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)
}
}
我可以很好地发送电子邮件,但是当我关闭邮件视图时,我看到了我的初始视图(没关系),问题是如果我再次尝试调用电子邮件视图(我的意思是尝试发送另一封电子邮件)程序不响应,我不能再关闭它,我认为我需要重新加载初始视图???
感谢您的帮助!
【问题讨论】: