I have a couple of view controllers that can send email and was hoping to cleanup the code using a protocol extension. Something like this:
protocol MailSender: class, MFMailComposeViewControllerDelegate {
func composeMail(config: MFMailComposeViewController -> ())
func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?)
func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?)
}
extension MailSender {
func composeMail(config: MFMailComposeViewController -> ()) {
let mailer = MFMailComposeViewController()
mailer.mailComposeDelegate = self
config(mailer)
presentViewController(mailer, animated: true, completion: .None)
}
// This method will never be called :(
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
/* handle result */
dismissViewControllerAnimated(true, completion: .None)
}
}In use it looks like:
class MyViewController: UIViewController, MailSender {
…
// when needed, compose mail…
composeMail() { m in
m.setSubject("title")
m.setMessageBody("message", isHTML: false)
}It works (to an extent), the major issue is that the `MFMailComposeViewControllerDelegate` method never gets called. Only thing I can think of which might cause the issue is that the delegate method is optional, but why that would prevent things working, I'm not sure? (If I put the delegate method inside the view controllers, then everything works, but the point of the exercise is to reduce code duplication, so this isn't particularly helpful).
Any suggestions would be most welcome. Cheers!