MFMailComposeViewController 不会关闭 (iOS10)

Posted

技术标签:

【中文标题】MFMailComposeViewController 不会关闭 (iOS10)【英文标题】:MFMailComposeViewController won't close (iOS10) 【发布时间】:2016-09-21 01:29:06 【问题描述】:

希望你一切都好。

我对 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)

      
    

【问题讨论】:

方法/函数和变量名按照惯例应该是驼峰式。例如,不要使用helpdesk_email_send,而是使用helpdeskEmailSend 【参考方案1】:

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

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)
    

尝试一次。

【讨论】:

您好 MK,感谢您的回复!我刚刚尝试了您发布的代码,XCode 显示错误,告诉我 controller.dismissViewControllerAnimated(true, completion: nil) 已替换为 controller.dismiss(animated: true, completion: nil)。有什么建议吗? 我认为您使用的是 swift > 2.2。所以我的想法是错误的。我已经用另一种曾经帮助过我的方法更新了答案。试试看。

以上是关于MFMailComposeViewController 不会关闭 (iOS10)的主要内容,如果未能解决你的问题,请参考以下文章