MFMailComposeViewController无法关闭

17

我正在尝试在我的应用程序中实现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和类似的服务上搜索过,但是没有找到任何答案。


didFinishWithResult被调用了吗?我的意思是你是否添加了委托? - Gokul G
1
我测试了你的代码。它完全正常运行。 - Kenan Karakecili
有点奇迹,因为它仍然无法正常工作。 - Pavel Zagorskyy
如果你还没有的话,尝试在实际设备上进行测试。 - Kenan Karakecili
我在我的iPhone上测试它,而不是在模拟器上。 - Pavel Zagorskyy
7个回答

13

Swift 5

记得要添加两个代理:

emailComposer.mailComposeDelegate = self
emailComposer.delegate = self

如果您只添加一个,它不会消失。同时确保实现了委托方法:

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

1
对我来说就像魔法一样有效!谢谢Ken。 - MS_iOS

9
如果你在使用 Swift 3.0 遇到了这个问题,我认为 MFMailComposeViewController 可能有两种看起来相似的方法可以解决这个问题。请确保使用正确的方法。
  func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }

@HusseinAlBehary 好棒! - Terry Bu
这对我也起作用了!由于某种原因,苹果在这里的示例 https://developer.apple.com/documentation/messageui/mfmailcomposeviewcontroller 没有。 - Abe

6
对于Swift 3,您需要添加以下内容:
        composer.mailComposeDelegate = self as MFMailComposeViewControllerDelegate

5

我通过删除以下内容的方式解决了这个问题:completion

extension UIViewController: MFMailComposeViewControllerDelegate {

    func sendEmail() {
      //send email
    }

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

}

0
假设mailComposeControllermailComposeController是与mailButtonDidPressedmailButtonDidPressed相同类的函数,那么应该是它的实例来关闭MFMailComposeViewController,所以使用

self.dismissViewControllerAnimated(true, completion: nil)

而不是

controller.dismissViewControllerAnimated(true, completion: nil)


我得到了完全相同的结果。 - Pavel Zagorskyy

0
我曾经遇到过这个问题,答案非常简单。请确保包含


MFMailComposeViewControllerDelegate

在列出您的继承权时,将其置于顶部,然后包括其他答案中的内容。


0

你没有添加委托:

emailComposer.delegate = self 

你的代码应该像这样:

func mailButtonDidPressed {
        ...

        let emailComposer = MFMailComposeViewController()
       emailComposer.delegate = self 

...
    }

3
我可以看到这行代码在 OP 的代码中:emailComposer.mailComposeDelegate = self - GoodSp33d
我不确定100%,但它不是一样的。 - Chlebta
1
emailComposer.delegate = self 这个代理应该符合UINavigationControllerDelegate协议,你确定我在我的视图控制器中需要符合这个协议吗? - Pavel Zagorskyy
请在 controller.dismissViewControllerAnimated(true, completion: nil) 处添加调试点,并向我展示调试器中的结果。 - Chlebta
1
@user3224534 看起来你正在从子视图控制器中呈现视图控制器。你可以尝试从父视图控制器中进行呈现,具体方法请参考这里 - GoodSp33d
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接