Reputation: 1029
I am trying to implement a send mail code but I'm getting the following error :
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Here is my code piece:
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["@gmail.com"])
presentViewController(mail, animated: true, completion: nil)
} else {
// show failure alert
}
Upvotes: 2
Views: 3507
Reputation: 9352
I think your "mail" object is going out of scope and getting deallocated before it is presented.
Try making "mail" a class-level var at the top of your view controller:
var mail: MFMailComposeViewController?
Then set and use it in your button action:
self.mail = MFMailComposeViewController()
mail!.mailComposeDelegate = self
//etc....
EDIT: The mail object is retained by the presentViewController function, so my answer is incorrect. Thanks to Daniel Zhang for pointing this out.
Upvotes: 1
Reputation: 5858
The error that you received could happen if you forgot to remove the segue to another function of your button.
Upvotes: 2