应用程序按钮打开电子邮件但未赢得关闭窗口并返回应用程序

时间:2015-12-15 23:50:14

标签: ios swift email mfmailcomposeviewcontroller

我的应用程序中有一个按钮,会打开一封要发送给我的电子邮件,当按下此按钮时,iPhone上的电子邮件应用程序会打开,按下发送后会发送电子邮件但窗口不会发送关闭然后返回我的应用程序。此外,当我按下取消时,它提供了保存/删除草稿的选项,但同样没有关闭窗口并返回到我的应用程序。我已在下面附上电子邮件代码。

@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)
}


}

2 个答案:

答案 0 :(得分:5)

看起来您忘记实施func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { controller.presentingViewController?.dismissViewControllerAnimated(true, completion: nil) } ,请添加:

public Obj * objPtr;

答案 1 :(得分:0)

管理修复它!

     @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)
}
}