在Swift 3.0迁移后不调用MFMailComposeViewControllerDelegate方法

时间:2016-10-26 10:16:37

标签: ios swift3 mfmailcomposer

我的应用程序具有电子邮件撰写功能,它在Swift 2.2上运行良好。最近我将代码迁移到Swift 3.0并坚持这个问题。在下面分享我的代码段:

import MessageUI

class ViewController: UIViewController,     MFMailComposeViewControllerDelegate {
 func sendEmail() {
    if MFMailComposeViewController.canSendMail() {
      let mail = MFMailComposeViewController()
      mail.mailComposeDelegate = self
      mail.setToRecipients(["someone@somewhere.com"])
      mail.setSubject("Sending you an in-app e-mail...")
      mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
      self.present(mail, animated: true)
    } else {
        //  handle failure
    }
  }

 func mailComposeController(_ controller:    MFMailComposeViewController, didFinishWith result: MFMailComposeResult,  error: Error?) {

    controller.dismiss(animated: true)
  }

 @IBAction func mailClicked(_ sender: AnyObject) {    
    sendEmail()
  }
 }

我已将mailComposeDelegate设置为self,将_放入委托方法并尝试搜索时找到的所有解决方案。但无法解决问题。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:7)

这是Xcode 8中的一个已知问题。以下解决方法对我有用:

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