MFMailComposeViewController不会关闭(iOS10)

我希望你很好。

我对Swift很陌生。 我使用iOS10 SDK在XCode 8.0上构建了一个iOS应用程序。 我试图添加允许用户发送电子邮件到预先配置的电子邮件地址的功能。 当点击发送按钮时,MFMailComposeViewController正常打开,用户可以通过其添加的邮件帐户发送或取消。 点击发送实际上通常会发送电子邮件,但是MFMailComposeViewController在发送邮件或点击取消按钮后不会关闭。 请帮忙!

谢谢

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)

      }
    }
}

用这个替换你的完整代码。

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)
    }
}

尝试一次。

链接地址: http://www.djcxy.com/p/94549.html

上一篇: MFMailComposeViewController won't close (iOS10)

下一篇: iOS: Memory leak codes