点击发送或取消按钮后如何关闭邮件视图控制器
how to dismiss mail view controller after tapping send or cancel button
在发送邮件时,点击发送或取消按钮后,视图控制器停留在那里并且应用停止。
//swift 2.2 ; xcode 7.3.1 ;
if( MFMailComposeViewController.canSendMail() ) {
print("Can send email.")
}
var subjectText = "Verification"
var toReceipients = ["notorious.roman@gmail.com"]
// var msgBody = "Verified"
var mc:MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(subjectText)
mc.setMessageBody("Verified", isHTML: false)
mc.setToRecipients(toReceipients)
self.presentViewController(mc, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
我想@rmaddy 在他的评论中回答了你的问题,不过我会向你解释发生了什么。您正试图忽略呈现 MFMailComposeViewController
而不是 MFMailComposeViewController
的 UIViewController
。
正如 Apple 在他的文档中指定的那样:
The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController:didFinishWithResult:error:
method of its delegate. Your implementation of that method must dismiss the view controller explicitly.
所以需要这样设置方法:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Dismiss the mail compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}
希望对您有所帮助。
Swift 4.0 更新。
Swift 5.0 更新。
请允许我在讨论中添加一些内容...
在 Swift 4 和 5 中,委托方法略有变化; 因为你现在发布了,不会有任何效果,也不会被调用。我遇到了,快把我逼疯了!
Xcode 警告提出了三个修复建议,但前两个可能具有误导性。这只是一个很小的修复...
这是为 Swift 3、4 和 5 修复的委托方法:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
不过,维克多的答案应该是 correct/accepted 那个。
尽情享受吧!
有一个为我控制它的 Switch 语句:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue :
print("Cancelled")
case MFMailComposeResult.failed.rawValue :
print("Failed")
case MFMailComposeResult.saved.rawValue :
print("Saved")
case MFMailComposeResult.sent.rawValue :
print("Sent")
default: break
}
self.dismiss(animated: true, completion: nil)
}
在发送邮件时,点击发送或取消按钮后,视图控制器停留在那里并且应用停止。
//swift 2.2 ; xcode 7.3.1 ;
if( MFMailComposeViewController.canSendMail() ) {
print("Can send email.")
}
var subjectText = "Verification"
var toReceipients = ["notorious.roman@gmail.com"]
// var msgBody = "Verified"
var mc:MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(subjectText)
mc.setMessageBody("Verified", isHTML: false)
mc.setToRecipients(toReceipients)
self.presentViewController(mc, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
我想@rmaddy 在他的评论中回答了你的问题,不过我会向你解释发生了什么。您正试图忽略呈现 MFMailComposeViewController
而不是 MFMailComposeViewController
的 UIViewController
。
正如 Apple 在他的文档中指定的那样:
The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the
mailComposeController:didFinishWithResult:error:
method of its delegate. Your implementation of that method must dismiss the view controller explicitly.
所以需要这样设置方法:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Dismiss the mail compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}
希望对您有所帮助。
Swift 4.0 更新。 Swift 5.0 更新。
请允许我在讨论中添加一些内容...
在 Swift 4 和 5 中,委托方法略有变化; 因为你现在发布了,不会有任何效果,也不会被调用。我遇到了,快把我逼疯了!
Xcode 警告提出了三个修复建议,但前两个可能具有误导性。这只是一个很小的修复...
这是为 Swift 3、4 和 5 修复的委托方法:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
不过,维克多的答案应该是 correct/accepted 那个。
尽情享受吧!
有一个为我控制它的 Switch 语句:
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue :
print("Cancelled")
case MFMailComposeResult.failed.rawValue :
print("Failed")
case MFMailComposeResult.saved.rawValue :
print("Saved")
case MFMailComposeResult.sent.rawValue :
print("Sent")
default: break
}
self.dismiss(animated: true, completion: nil)
}