我有一个MFMailComposeViewController设置,如下所示:
class MyViewController: UIViewController {
// MARK: - Properties
let composeViewController = MFMailComposeViewController()
// MARK: - Actions
@IBAction func didTapSendInEmailButton() {
composeViewController.mailComposeDelegate = self
composeViewController.setToRecipients([Constants.contactRecipientEmail])
composeViewController.setSubject(Constants.contactSubject)
composeViewController.setMessageBody(Constans.body, isHTML: false)
present(composeViewController, animated: true, completion: nil)
}
}
// MARK: - MFMailComposeViewControllerDelegate
extension MyViewController: MFMailComposeViewControllerDelegate {
private func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Error?) {
switch result {
case .sent:
print("Email sent")
case .saved:
print("Draft saved")
case .cancelled:
print("Email cancelled")
case .failed:
print("Email failed")
}
controller.dismiss(animated: true, completion: nil)
}
}
我遇到了这个问题:
答案 0 :(得分:2)
extension MyViewController: MFMailComposeViewControllerDelegate {
private func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Error?)
再次检查您的委托方法,它应该是public
。
还要确保您的邮件帐户配置正确。
答案 1 :(得分:2)
我不知道你的问题在哪里
我测试你的代码它运行得很好,编译器没有强制我将代理设置为私有,注意电子邮件编写器不能在simulator
只在device
上工作
import UIKit
import MessageUI
class MyViewController: UIViewController {
// MARK: - Properties
let composeViewController = MFMailComposeViewController()
// MARK: - Actions
@IBAction func didTapSendInEmailButton() {
if MFMailComposeViewController.canSendMail() {
composeViewController.mailComposeDelegate = self
composeViewController.setToRecipients(["abdela7ad@gmail.com"])
composeViewController.setSubject("Constants.contactSubjec")
composeViewController.setMessageBody("Constans.body", isHTML: false)
present(composeViewController, animated: true, completion: nil) } else {
}
}
}
// MARK: - MFMailComposeViewControllerDelegate
extension MyViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .sent:
print("Email sent")
case .saved:
print("Draft saved")
case .cancelled:
print("Email cancelled")
case .failed:
print("Email failed")
}
controller.dismiss(animated: true, completion: nil)
}
}
答案 2 :(得分:2)
我将在评论中强调一些小的改动:
import UIKit
import MessageUI
class MyViewController: UIViewController {
// MARK: - Properties
let composeViewController = MFMailComposeViewController()
// MARK: - Actions
@IBAction func didTapSendInEmailButton() {
composeViewController.mailComposeDelegate = self
// Entered a generic email in place of your constant value
composeViewController.setToRecipients(["someone@example.com"])
// Entered a generic subject in place of your constant value
composeViewController.setSubject("subject")
// You have a typo on "Constants" here
composeViewController.setMessageBody("body", isHTML: false)
present(composeViewController, animated: true, completion: nil)
}
}
// MARK: - MFMailComposeViewControllerDelegate
extension MyViewController: MFMailComposeViewControllerDelegate {
// Removed the private
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Error?) {
switch result {
case .sent:
print("Email sent")
case .saved:
print("Draft saved")
case .cancelled:
print("Email cancelled")
case .failed:
print("Email failed")
}
controller.dismiss(animated: true, completion: nil)
}
}
我的控制台日志显示已发送电子邮件。
您确认您的Constants对象包含有效数据吗?也许您应该将它打印出来或在到达此处时在断点处查看它以确保其内容没有问题。
此外,请确保您首先在设备上设置了有效的电子邮件帐户,以便为您发送电子邮件。如果它是一个开发设备,那么它可能在某个时候被重置并丢失。
请注意Error
的颜色会发生变化。这意味着它正在使用您已定义的错误,该错误的范围是私有的。您需要通过将错误重命名为其他内容来解决此问题。完成后,它应该可以解决您的错误。
答案 3 :(得分:1)
我最近也遇到了类似的问题,我的工作是我在按钮操作中移动了声明
class MyViewController: UIViewController {
// MARK: - Actions
@IBAction func didTapSendInEmailButton() {
let composeViewController = MFMailComposeViewController()
composeViewController.mailComposeDelegate = self
composeViewController.setToRecipients([Constants.contactRecipientEmail])
composeViewController.setSubject(Constants.contactSubject)
composeViewController.setMessageBody(Constans.body, isHTML: false)
present(composeViewController, animated: true, completion: nil)
}
}
// MARK: - MFMailComposeViewControllerDelegate
extension MyViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult,
error: Error?) {
switch result {
case .sent:
print("Email sent")
case .saved:
print("Draft saved")
case .cancelled:
print("Email cancelled")
case .failed:
print("Email failed")
}
controller.dismiss(animated: true, completion: nil)
}
}
此外,您不需要将委托方法设为私有