Ios 电子邮件已完成,但“发送电子邮件”窗口未关闭?

Ios 电子邮件已完成,但“发送电子邮件”窗口未关闭?,ios,swift,mfmailcomposeviewcontroller,Ios,Swift,Mfmailcomposeviewcontroller,我的问题是,我对emailwindow进行了编程,它确实会发送电子邮件,但当它们被发送或我想关闭窗口时,什么都不会发生 这是我的密码: import Foundation import UIKit import MessageUI class ContactViewController: UIViewController, MFMailComposeViewControllerDelegate, UIAlertViewDelegate { override func viewDid

我的问题是,我对emailwindow进行了编程,它确实会发送电子邮件,但当它们被发送或我想关闭窗口时,什么都不会发生

这是我的密码:

import Foundation
import UIKit
import MessageUI


class ContactViewController: UIViewController, MFMailComposeViewControllerDelegate, UIAlertViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

let mail = MFMailComposeViewController()


    @IBAction func email(_ sender: Any) {

        if !MFMailComposeViewController.canSendMail() {
            let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert)
            let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
            warnung.addAction(action1)
            self.present(warnung, animated: true, completion: nil)
            return

            } else {

                mail.mailComposeDelegate = self
                mail.setToRecipients(["team@example.com"])
                mail.setSubject("Message to you")
                mail.setMessageBody("Hello,\n", isHTML: false)

                present(mail, animated: true, completion: nil)

            func mailComposeController(_ controller: MFMailComposeViewController,
                                       didFinishWithResult result: MFMailComposeResult, error: NSError?) {
                mail.dismiss(animated: true, completion: nil)
                print("Yes!")
                }


            }
        }
}
以下是邮件窗口的屏幕截图:

为什么委托方法在
电子邮件(\u sender:)
方法中,在它的
else
测试中?因为我认为它在else方法中工作得更好请注意,您的方法
didfishwithresult
是在iAction电子邮件方法中声明的。您需要将该方法移出iAction方法。它需要是视图控制器的实例方法
@IBAction func email(_ sender: Any) {

    if !MFMailComposeViewController.canSendMail() {
        let warnung = UIAlertController(title: "Email konnte nicht gesendet werden", message: "Dein Gerät unterstützt leider keine Email-Funktion.", preferredStyle: .alert)
        let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
        warnung.addAction(action1)
        self.present(warnung, animated: true, completion: nil)
        return

        } else {

            mail.mailComposeDelegate = self
            mail.setToRecipients(["team@example.com"])
            mail.setSubject("Message to you")
            mail.setMessageBody("Hello,\n", isHTML: false)

            present(mail, animated: true, completion: nil)
        }
    }
}

func mailComposeController(_ controller: MFMailComposeViewController,
                                   didFinishWithResult result: MFMailComposeResult, error: NSError?) {
            mail.dismiss(animated: true, completion: nil)
            print("Yes!")
}