我正在尝试关闭MFMailComposeViewController,但是未触发该委托。看来这是一个普遍的问题,答案是相同的,但是它们对我不起作用:(。我有一个按钮调用该函数来发送邮件。我首先创建一个csv文件,然后创建MFMailComposeViewController并附加csv文件发送到邮件。有时会发送邮件(此后邮件视图控制器不会关闭),并且“取消”按钮向我显示了删除或保存草稿的选项,但此后什么也没发生。
这是按钮的代码:
import UIKit
import MessageUI
class UserInfoViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func uploadPressed(_ sender: Any) {
let contentsOfFile = "Name,ID,Age,Sex,Time,\n\(name),\(id),\(age),\(sex),\(time)"
let data = contentsOfFile.data(using: String.Encoding.utf8, allowLossyConversion: false)
if let content = data {
print("NSData: \(content)")
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let emailController = MFMailComposeViewController()
//emailController.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate
emailController.mailComposeDelegate = self
emailController.setToRecipients([""])
emailController.setSubject("CSV File")
emailController.setMessageBody("", isHTML: false)
emailController.addAttachmentData(data!, mimeType: "text/csv", fileName: "registro.csv")
return emailController
}
let emailViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.present(emailViewController, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
print("Delegate worked!")
controller.dismiss(animated: true, completion: nil)
}
}
}
非常感谢您。
答案 0 :(得分:1)
您的问题是由将委托方法放入另一个方法引起的。你不能那样做。委托功能必须位于类的顶层。只需将mailComposeController(_:didFinishWith:error:)
函数移出uploadPressed
函数即可。