MailComposer没有被解雇

时间:2017-05-20 06:03:21

标签: ios swift viewcontroller dismiss mfmailcomposer

我尝试使用MailComposer。这是我使用的代码:

func setupMailer() {
    if MFMailComposeViewController.canSendMail() {
        emailController.mailComposeDelegate = self
        emailController.setToRecipients([]) // set the email address
        emailController.setSubject("BackgroundTask Test")
        emailController.setMessageBody("Message body", isHTML: false)
    }
}

然后当用户按下按钮时:

func buttonPressed(button: UIButton) {
    debugPrint("buttonPressed", button)
    let path = dirpath.appendingPathComponent(filename)
    let data = NSData(contentsOfFile: path.path)
    emailController.mailComposeDelegate = self
    emailController.addAttachmentData(data! as Data, mimeType: "text/csv", fileName: filename)
    present(emailController, animated: true, completion: nil)
}

当解雇时:

@objc func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    debugPrint("mailComposeController dismiss")
    controller.dismiss(animated: true, completion: nil)
}

发现如果第一次按下该按钮,无论我选择发送还是取消,邮件编辑器都能正常工作。

然而,在我发送/取消后,第二次,邮件作曲家不能被解雇。发送的响应可以发送电子邮件,但邮件编辑器界面永远不会解散。

我发现第一次之后不再触发函数func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)

任何线索?

3 个答案:

答案 0 :(得分:2)

我不知道你做错了什么。 我认为你正在使用Swift。所以我会解释你如何实现它。步骤进行:

1)import MessagesUI并添加委托MFMailComposeViewControllerDelegate

2)添加此功能:

func configuredMailComposeViewController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setToRecipients(["someone@somewhere.com"])
    mailComposerVC.setSubject("Sending you an in-app e-mail...")
    mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

    return mailComposerVC
}

3)将按钮的IBAction中的上述功能调用为:

let mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.present(mailComposeViewController, animated: true, completion: nil)
    } else {
        // Show alert if user can't send mail
    }

4)最后实现委托方法:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true, completion: nil)
}

答案 1 :(得分:1)

您不应该再次使用相同的MFMailComposeViewController实例..

试试这个

func setupMailer() {
    if MFMailComposeViewController.canSendMail() {
        emailController = MFMailComposeViewController.init()
        emailController.mailComposeDelegate = self
        emailController.setToRecipients([]) // set the email address
        emailController.setSubject("BackgroundTask Test")
        emailController.setMessageBody("Message body", isHTML: false)
    }
}

答案 2 :(得分:1)

是的,重点是我重用了emailController实例。

  func setupMailer() {
if MFMailComposeViewController.canSendMail() {
  emailController = MFMailComposeViewController()
  emailController.mailComposeDelegate = self
  emailController.setToRecipients([]) // set the email address
  emailController.setSubject("BackgroundTask Test")
  emailController.setMessageBody("Message body", isHTML: false)
}

}

  func buttonPressed(button: UIButton) {
    debugPrint("buttonPressed", button)
    let path = dirpath.appendingPathComponent(filename)
    let data = NSData(contentsOfFile: path.path)
    setupMailer()
    emailController.addAttachmentData(data! as Data, mimeType: "text/csv", fileName: filename)
    present(emailController, animated: true, completion: nil)
  }

  @objc func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    debugPrint("mailComposeController dismiss")
    controller.dismiss(animated: true, completion: nil)
  }

现在可以使用了。