MFMessageComposeViewController cancel button not working
Here's the answer for Swift 4, iOS 11
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
print("Emailing attempt, error="+(error?.localizedDescription)!)
switch (result){
case MFMailComposeResult.cancelled:
print("Mail cancelled");
break;
case MFMailComposeResult.saved:
print("Mail saved");
break;
case MFMailComposeResult.sent:
print("Mail sent");
break;
case MFMailComposeResult.failed:
print("Mail sent failure: %@", error?.localizedDescription);
break;
default:
break;
}
// Close the Mail Interface
controller.dismiss(animated: true)
}
If anyone needs it, the messageComposeDelegate method translated into Swift (3):
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true)
}
Did you forget to implement mailComposeController:didFinishWithResult:
?...
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[controller dismissViewControllerAnimated:YES completion:nil];
}
From apple documentation:
// The mail compose view controller delegate method
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES];
}
But you can handle all cases depend on your goal ...
The correct answer is to set the controller's messageComposeDelegate
, and to define the following delegate method:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(@"Message cancelled");
else if (result == MessageComposeResultSent)
NSLog(@"Message sent");
else
NSLog(@"Message failed");
}
@Doro's answer is based on the MFMailComposeViewController
not the MFMessageComposeViewController