I use this block to send message to the contacts, after sending, the back button is there, but when I touch it nothing happens. Please help me out :)
-(IBAct
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 ...
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)
}
Here's the answer for Swift 4, iOS 11
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;
}
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
Like the answers say, you need to dismiss in the delegate. My issue was I was setting delegate
instead of messageComposeDelegate
(or mailComposeDelegate
for the e-mail version).
Be sure you're setting the right delegate :)