我在具有以下代码的视图控制器中使用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
显示如预期,但具有以下行为:
MFMailComposeViewController
这是Xcode 11.2,iOS 13.2,Swift 4。
我该如何解决?
在此代码中
extension MyViewController : MFMailComposeViewControllerDelegate {
private func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
删除关键字private
。它对Cocoa隐藏了该方法,因此它将永远不会被调用。
我认为您需要打开结果以检查所有案例并根据结果案例执行相关操作。
重要:您必须使您的ViewController为mailComposeDelegate
例如:==>yourMailComposerInstance.mailComposeDelegate = self
其中self
是您的托管ViewController等。
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true) {
switch result {
case .cancelled:
print("Cancelled")
case .saved:
print("Saved")
case .sent:
print("Sent")
case .failed:
print("Failed.")
@unknown default:
print("Unknown")
}
}
}