MFMailComposeViewController won't close (iOS10)
I hope you're well.
I'm very new to Swift. I've built an iOS application on XCode 8.0 using the iOS10 SDK. I'm trying to add the ability to allow a user to send an email to a preconfigured email address. When tapping the send button, the MFMailComposeViewController opens normally and the user is presented with the ability to send or cancel via their added mail account. Clicking send actually sends the email normally, but the MFMailComposeViewController won't close after sending an email or when clicking the cancel button. Please help!
Thanks
import UIKit
import MessageUI
class EmailHelpdesk_ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Email Subject and Body text fields.
@IBOutlet var helpdesk_email_subject: UITextField!
@IBOutlet var helpdesk_email_body: UITextView!
//Helpdesk (Submit Button - Config)
@IBAction func helpdesk_email_send(_ sender: AnyObject) {
let HDsubjectText = helpdesk_email_subject.text
let HDbodyText = helpdesk_email_body.text
MFMailComposeViewController to be called.
let mail_controller_compose = MFMailComposeViewController()
mail_controller_compose.mailComposeDelegate = self
mail_controller_compose.setToRecipients(["example@example.com"])
mail_controller_compose.setSubject(HDsubjectText!)
mail_controller_compose.setMessageBody(HDbodyText!, isHTML: false)
self.present(mail_controller_compose, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result:MFMailComposeResult, error: NSError?) {
controller.dismiss(animated: true, completion: nil)
}
}
}
replace your complete code by this.
import UIKit
import MessageUI
class EmailHelpdesk_ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Email Subject and Body text fields.
@IBOutlet var helpdesk_email_subject: UITextField!
@IBOutlet var helpdesk_email_body: UITextView!
var mail_controller_compose:MFMailComposeViewController!
//Helpdesk (Submit Button - Config)
@IBAction func helpdesk_email_send(_ sender: AnyObject) {
let HDsubjectText = helpdesk_email_subject.text
let HDbodyText = helpdesk_email_body.text
//MFMailComposeViewController to be called.
mail_controller_compose = MFMailComposeViewController()
mail_controller_compose.mailComposeDelegate = self
mail_controller_compose.setToRecipients(["example@example.com"])
mail_controller_compose.setSubject(HDsubjectText!)
mail_controller_compose.setMessageBody(HDbodyText!, isHTML: false)
self.present(mail_controller_compose, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result:MFMailComposeResult, error: NSError?) {
mail_controller_compose.dismiss(animated: true, completion: nil)
}
}
Try once.
链接地址: http://www.djcxy.com/p/94550.html