如何关闭MFMailComposeViewController? [英] How to close MFMailComposeViewController?

查看:91
本文介绍了如何关闭MFMailComposeViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Swift应用程序发送一封电子邮件,并且效果很好,它可以发送电子邮件.

I want to send an email from my Swift application and it works well, it sends the email.

不过,发送电子邮件后,布局不会退出.我想在发送电子邮件后退出布局.当我按下取消",删除草稿"或保存草稿"按钮时,我也想要这种行为.

Nevertheless, after sending the email, the layout does not quits. I want to quit the layout after sending the email. I also want this behaviour when I press on Cancel, Remove draft or Save draft button.

这是我发送电子邮件的代码(按下按钮后):

This is the code that I have to send the email (after pressing a button):

@IBAction func btnSendEmailAction(_ sender: AnyObject) {

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self

    composeVC.setToRecipients(["mymail@mail.com"])
    composeVC.setSubject("Hello!")
    composeVC.setMessageBody("Hello World!", isHTML: false)

    self.present(composeVC, animated: true, completion: nil)
}

func mailComposeController(controller: MFMailComposeViewController,
                           didFinishWithResult result: MFMailComposeResult, error: NSError?) {

    switch result {

    case MFMailComposeResult.cancelled:
        controller.dismiss(animated: true, completion: nil)
        break

    case MFMailComposeResult.sent:
        controller.dismiss(animated: true, completion: nil)
        break

    case MFMailComposeResult.failed:
        controller.dismiss(animated: true, completion: nil)
        break

    default:
        break
    }

    controller.dismiss(animated: true, completion: nil)
}

但是当我按下取消"或已发送"按钮时,布局不会退出.

but the layout does not quits when I press on Cancel or Sent buttons.

我知道有很多与此问题相关的问题,但我查找了很多问题,这是我可以从其中一些问题中获得的代码.请注意,它们大多数在Objective中而不是Swift中(有时这些方法不存在).

I know that there are a lot of questions related about this problem but I looked a lot of them and this is the code I could get from a mix of some of them. Note that most of them are in Objective instead of Swift (and sometimes the methods does not exist).

示例: iPhone:如何关闭MFMailComposeViewController?

我在代码中缺少什么吗?如何检测删除草稿"和保存草稿"事件?

Am I missing something on my code? How can I detect Remove draft and Save Draft events?

提前谢谢!

推荐答案

好像您使用的是swift 3而不是有效的委托方法.纠正的委托方法是这样的:

Looks like you're using swift 3 and not using a valid delegate method. Corrected delegate method is this:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {

        switch result {
        case .cancelled:
            break
        case .saved:
            break
        case .sent:
            break
        case .failed:
            break

        }

        dismiss(animated: true, completion: nil)
    }

这篇关于如何关闭MFMailComposeViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆