MFMailComposeViewController 不发送电子邮件或关闭
MFMailComposeViewController does not send email or dismiss
我在视图控制器中使用 MFMailComposeViewController
,代码如下:
if !MFMailComposeViewController.canSendMail() {
return
}
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
present(mailComposeViewController, animated: true)
并且:
extension MyViewController : MFMailComposeViewControllerDelegate {
private func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
MFMailComposeViewController
按预期显示,但具有以下行为:
- 取消按钮不执行任何操作(如果消息未被编辑)或显示 "Delete Draft"/"Save Draft" 操作 sheet、none 选项关闭
MFMailComposeViewController
- 无论是禁用(未设置收件人)还是启用(设置收件人),发送按钮什么都不做
- 视图可以通过向下滑动来关闭(新的 iOS 13 模态行为)
这是Xcode11.2,iOS13.2,Swift4.
我该如何解决这个问题?
在此代码中
extension MyViewController : MFMailComposeViewControllerDelegate {
private func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
删除关键字private
。它隐藏了 Cocoa 的方法,因此它永远不会被调用。
确保您的 class MyViewController 不是 MFMailComposeViewController 的子class。
你应该:
- 创建一个 UIViewController 子class(class MailViewController: UIViewController)
- 添加 MFMailComposeViewController。
- MailViewController: MFMailComposeViewControllerDelegate
或者:
直接从另一个 UIViewController 配置 MFMailComposeViewController()。例如:
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients([recipient])
mailComposeVC.setSubject(subject)
mailComposeVC.setMessageBody(body, isHTML: false)
present(mailComposeVC, animated: true, completion: nil)
我在视图控制器中使用 MFMailComposeViewController
,代码如下:
if !MFMailComposeViewController.canSendMail() {
return
}
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
present(mailComposeViewController, animated: true)
并且:
extension MyViewController : MFMailComposeViewControllerDelegate {
private func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
MFMailComposeViewController
按预期显示,但具有以下行为:
- 取消按钮不执行任何操作(如果消息未被编辑)或显示 "Delete Draft"/"Save Draft" 操作 sheet、none 选项关闭
MFMailComposeViewController
- 无论是禁用(未设置收件人)还是启用(设置收件人),发送按钮什么都不做
- 视图可以通过向下滑动来关闭(新的 iOS 13 模态行为)
这是Xcode11.2,iOS13.2,Swift4.
我该如何解决这个问题?
在此代码中
extension MyViewController : MFMailComposeViewControllerDelegate {
private func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
删除关键字private
。它隐藏了 Cocoa 的方法,因此它永远不会被调用。
确保您的 class MyViewController 不是 MFMailComposeViewController 的子class。
你应该:
- 创建一个 UIViewController 子class(class MailViewController: UIViewController)
- 添加 MFMailComposeViewController。
- MailViewController: MFMailComposeViewControllerDelegate
或者:
直接从另一个 UIViewController 配置 MFMailComposeViewController()。例如:
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients([recipient])
mailComposeVC.setSubject(subject)
mailComposeVC.setMessageBody(body, isHTML: false)
present(mailComposeVC, animated: true, completion: nil)