MFMailComposeViewController, Swift 4, Xcode 9

你。 提交于 2019-12-23 20:08:38

问题


I have a problem with MFMailComposeViewControllerDelegate function.

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

The warning says

Instance method 'mailComposeController(:didFinishWith:error:)' nearly matches optional requirement 'mailComposeController(:didFinishWith:error:)' of protocol 'MFMailComposeViewControllerDelegate'

Make 'mailComposeController(_:didFinishWith:error:)' private to silence this warning

I need to return the user to the App and dismiss MFMailComposeViewController after clicking cancel and this function is not triggered.

Yes, I added the delegate composeVC.mailComposeDelegate = self

If someone had a similar problem, I would appreciate the help. Thanks

EDIT

This behavior is happening only when I set the language to Swift 4. I just went back few commits and it's working perfectly fine with Swift 3.2

Basically, this is the code:

class TechSupportVC: UIViewController, MFMailComposeViewControllerDelegate {
    let composeVC = MFMailComposeViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    composeVC.mailComposeDelegate = self
    composeVC.setToRecipients(["desiredEmail@gmail.com"])
    composeVC.setSubject("My message")
}

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

@IBAction func sendPressed(_ sender: Any) {
    guard MFMailComposeViewController.canSendMail() else {
        showMailServiceErrorAlert()
        return
    }

    composeVC.setMessageBody("Test credentials: \(firstAndLastNameTextField.text!)\nPhone: \(numberTextField.text!)\n\n\(messageTextView.text!)", isHTML: false)

    self.present(composeVC, animated: true, completion: nil)
}

}


回答1:


It wasn't possible for me to apply Đorđe's solution, this other answer helped me.

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

Adding Swift. prefix to Error? solves the problem.




回答2:


I had the same issue where the mailComposeControler delegate wasn't getting called after canceling or sending the mail. xCode gave me the same warning about adding private. I did not have an issue an enumeration named Error as the others had.

The only way I could fix it was to specifically define the function as public.

public func mailComposeController(_ controller: MFMailComposeViewController,
                                  didFinishWith result: MFMailComposeResult, error: Error?) {
    // Check the result or perform other tasks.

    // Dismiss the mail compose view controller.
    self.dismiss(animated: true, completion: nil)
}

After doing that it worked fine.

This was in Swift 4.0, xCode 10.1.




回答3:


EDIT:

After adding other classes to the project I encountered the same problem again and realized that conversion was not the problem.

The problem was that I have an enumeration named Error, that's why the parameter error: was not recognizing the Swift Error class, so the warning was correct.

I came to edit the answer and I saw that Boris Y. wrote the fix for this, so I'll accept his answer.



来源:https://stackoverflow.com/questions/48703157/mfmailcomposeviewcontroller-swift-4-xcode-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!