应用程序中的按钮打开电子邮件,但不会关闭应用程序的 window 和 return

button from app opens email but won't close the window and return to app

我的应用程序中有一个按钮,可以打开要发送给我的电子邮件,按下此按钮时,iPhone 上的电子邮件应用程序将打开,按下发送按钮后,电子邮件将被发送,但是window 没有关闭然后 return 到我的应用程序。此外,当我按取消时,它会提供 save/delete 草稿选项,但不会再次关闭 window 和 return 到我的应用程序。我附上了下面的电子邮件代码。

@IBAction func SendMessage(sender: AnyObject) {

var mail: MFMailComposeViewController!

let toRecipients = ["usalim76@gmail.com"]
let subject = "Enquiry"
let body = "Your body text"

mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(toRecipients)
mail.setSubject(subject)
mail.setMessageBody(body, isHTML: true)

presentViewController(mail, animated: true, completion: nil)

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
  controller.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}


}

您似乎忘记实施 MFMailComposeViewControllerDelegate,请添加:

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
      controller.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)       
}

设法修复它!

     @IBAction func SendMessage(sender: AnyObject) {

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

func configuredMailComposeViewController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

    mailComposerVC.setToRecipients(["someone@somewhere.com"])
    mailComposerVC.setSubject("Sending you an in-app e-mail...")
    mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

    return mailComposerVC
}

func showSendMailErrorAlert() {
    let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
    sendMailErrorAlert.show()
}

// MARK: MFMailComposeViewControllerDelegate Method
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}
}