uz7
uz7

Reputation: 159

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

I have a button in my app which opens up an email to be sent to me, when this button is pressed the email app on iPhone opens up and when sent is pressed the email is sent however the window doesn't close and then return to my app. Also when i press cancel it gives the option to save/delete draft but again doesn't close the window and return to my app. I have attached the email code below.

@IBAction func SendMessage(sender: AnyObject) {

var mail: MFMailComposeViewController!

let toRecipients = ["[email protected]"]
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)
}


}

Upvotes: 0

Views: 920

Answers (2)

uz7
uz7

Reputation: 159

Managed to fix it!

     @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(["[email protected]"])
    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)
}
}

Upvotes: 0

Joseph Afework
Joseph Afework

Reputation: 269

looks like you forgot to implement the MFMailComposeViewControllerDelegate, add this:

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

Upvotes: 5

Related Questions