UIButton 不调出 MFMailViewController

UIButton does not bring up MFMailViewController

我的一些代码有误。我对此很陌生,我正在努力自学。我在网上找到大部分答案,但似乎找不到关于这个特定问题的任何信息。我想在应用程序中发送一封电子邮件,但只要我按下电子邮件按钮,MFMailViewController 就不会出现。就像我的 UIButton 不工作一样。但我知道我把它作为 IBAction。到目前为止,这是我的代码。非常感谢任何帮助。

import UIKit
import MessageUI

class RequestService: UIViewController,MFMailComposeViewControllerDelegate {
    @IBOutlet weak var CustomerName: UITextField!
    @IBOutlet weak var emailButton: UIButton!

    @IBAction func sendEmail(_ sender: UIButton) {
        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")

            let ComposeVC = MFMailComposeViewController()
            ComposeVC.mailComposeDelegate = self

            ComposeVC.setToRecipients(["jwelch@ussunsolar.com"])
            ComposeVC.setSubject("New Support Ticket")
            ComposeVC.setMessageBody(CustomerName.text!, isHTML: false)

            self.present(ComposeVC, animated: true, completion: nil)
        }

        func mailComposeController(controller: MFMailComposeViewController,didFinishWithResult result:MFMailComposeResult, error: NSError?) {
            // Check the result or perform other tasks.
            // Dismiss the mail compose view controller.
            controller.dismiss(animated: true, completion: nil)
        }
    }
}

如果设备不能发送电子邮件,您只能尝试显示邮件控制器。那倒退了。

@IBAction func sendEmail(_ sender: UIButton) {
    if MFMailComposeViewController.canSendMail() {
        print("Mail services are not available")

        let ComposeVC = MFMailComposeViewController()
        ComposeVC.mailComposeDelegate = self

        ComposeVC.setToRecipients(["jwelch@ussunsolar.com"])
        ComposeVC.setSubject("New Support Ticket")
        ComposeVC.setMessageBody(CustomerName.text!, isHTML: false)

        self.present(ComposeVC, animated: true, completion: nil)
    }
}

func mailComposeController(controller: MFMailComposeViewController,didFinishWithResult result:MFMailComposeResult, error: NSError?) {
    // Check the result or perform other tasks.
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}

并且您需要在其他方法之外使用委托方法。

您的 sendMail 函数中的语法有误。如果设备不能发送邮件,您发布的代码将只打开视图控制器。将其更改为:

@IBAction func sendEmail(_ sender: UIButton) {
    if !MFMailComposeViewController.canSendMail() {
        print("Mail services are not available")
        return
    }

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self
    composeVC.setToRecipients(["jwelch@ussunsolar.com"])
    composeVC.setSubject("New Support Ticket")
    composeVC.setMessageBody(CustomerName.text!, isHTML: false)

    self.present(composeVC, animated: true, completion: nil)
}