I have this code the problem its about didFinishWith result doesn't called and the app crashes. This is my code
Note: In Objective C this code works fine because I create a strong reference in my class but I have a problem in Swift and I dont know how to solve this
import Foundation
import MessageUI
class EmailCreator: NSObject, MFMailComposeViewControllerDelegate {
// in other class I send this var to show email
var viewToShow = UIViewController ()
func sendEmail() {
let mailComposeViewController = createMailComposeViewController()
if MFMailComposeViewController.canSendMail(){
viewToShow.present(mailComposeViewController, animated: true, completion: nil)
}else{
print("Can't send email")
}
}
func createMailComposeViewController() -> MFMailComposeViewController {
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setToRecipients(["[email protected]"])
mailComposeViewController.setSubject("subject")
mailComposeViewController.setMessageBody("test body", isHTML: false)
return mailComposeViewController
}
//MARK: - MFMail compose method
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
And here in other class I have this code to show the email:
@IBAction func sendEmail(_ sender: UIButton) {
let email = EmailCreator()
email.viewToShow = self
email.sendEmail()
}
It crashes because you have the
EmailCreator
, a local var the delegate ofMFMailComposeViewController
as shown in yourfunc createMailComposeViewController
. By the timeMFMailComposeViewController
calls thedidFinishWith
method theEmailCreator
is alreadydeinit
ed. You can fix this by making yourEmailCreator
instance a strong property.