MFMailComposeViewController不会被忽略

时间:2016-08-09 13:16:15

标签: ios iphone swift email mfmailcomposeviewcontroller

我尝试在我的应用程序中发送电子邮件时实施MFMailComposeViewController。问题是,在提交MFMailComposeViewController之后,它并没有被"取消"或"发送"按钮,只是稍微向上滚动。

以下是它的介绍:

func mailButtonDidPressed {
        let emailTitle = "Test email"
        let messageBody = "some body bla bla bla"
        let toRecipents = "email@gmail.com"

        let emailComposer = MFMailComposeViewController()
        emailComposer.setSubject(emailTitle)
        emailComposer.setMessageBody(messageBody, isHTML: false)
        emailComposer.setToRecipients([toRecipents])
        emailComposer.mailComposeDelegate = self
        self.presentViewController(emailComposer, animated: true, completion: nil)
    }

并解雇委托代码:

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    switch (result) {
    case MFMailComposeResultSent:
        print("You sent the email.")
        break
    case MFMailComposeResultSaved:
        print("You saved a draft of this email")
        break
    case MFMailComposeResultCancelled:
        print("You cancelled sending this email.")
        break
    case MFMailComposeResultFailed:
        print("Mail failed:  An error occurred when trying to compose this email")
        break
    default:
        print("An error occurred when trying to compose this email")
        break
    }

    controller.dismissViewControllerAnimated(true, completion: nil)
}

我浏览过StackOverflow和其他类似的服务,但找不到任何答案。

7 个答案:

答案 0 :(得分:4)

如果有任何人在Swift 3.0中遇到此问题,我认为MFMailComposeViewController可能有两种类似于CORRECT方法的方法。

确保使用此方法

  func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }

答案 1 :(得分:4)

对于swift 3你必须添加

        composer.mailComposeDelegate = self as MFMailComposeViewControllerDelegate

答案 2 :(得分:1)

我解决了以这种方式删除completion的写法:

extension UIViewController: MFMailComposeViewControllerDelegate {

    func sendEmail() {
      //send email
    }

    public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }

}

答案 3 :(得分:1)

雨燕5

记住要添加两个代表:

emailComposer.mailComposeDelegate = self
emailComposer.delegate = self

如果仅添加一个,则不会关闭。还要确保实现委托方法:

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

答案 4 :(得分:0)

假设mailComposeControllermailComposeController是与mailButtonDidPressedmailButtonDidPressed相同的类的函数,它应该是它关闭MFMailComposeViewController的实例,所以

self.dismissViewControllerAnimated(true, completion: nil)

而不是

controller.dismissViewControllerAnimated(true, completion: nil)

答案 5 :(得分:0)

您没有添加委托:

emailComposer.delegate = self 

你的代码应该是这样的:

func mailButtonDidPressed {
        ...

        let emailComposer = MFMailComposeViewController()
       emailComposer.delegate = self 

...
    }

答案 6 :(得分:0)

我遇到了这个问题,答案非常简单。一定要包括

MFMailComposeViewControllerDelegate

在顶部,就像列出您的遗产一样,然后包含其他答案中的内容。