MFMailComposeViewController委托不在swift 4上工作

问题描述 投票:0回答:1

我正在尝试关闭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)

        }

    }
}

非常感谢你提前。

ios swift delegates mfmailcomposeviewcontroller
1个回答
1
投票

您的问题是由委托方法放在另一个方法中引起的。你不能这样做。委托函数需要位于类的顶层。只需将mailComposeController(_:didFinishWith:error:)函数移出uploadPressed函数即可。

© www.soinside.com 2019 - 2024. All rights reserved.