MFMessageComposeViewController cancel button not working

9.5k views Asked by At

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 :)

-(IBAction) InviteIt:(id) sender{


    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *messageComposer =
        [[MFMessageComposeViewController alloc] init];
        messageComposer.messageComposeDelegate = self;
        NSString *message = @"You have more body buddies than you think at: http://www.itunes.com/app/JoyChain ";
        [messageComposer setBody:message];
        messageComposer.recipients = [NSArray arrayWithObjects:_itsnum, nil];
        messageComposer.messageComposeDelegate = self;
        [self presentViewController:messageComposer animated:YES completion:nil];

    }

}
6

There are 6 answers

2
Doro On BEST ANSWER

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 ...

0
Sam Soffes On

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 :)

0
BananaNeil On

The correct answer is to set the controller's messageComposeDelegate, and to define the following delegate method:

- (void)messageComposeViewController:(MFMessageComposeViewCont‌​roller *)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

0
Josh On

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;
        }
0
Josh On

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)
    }
0
teacup On

If anyone needs it, the messageComposeDelegate method translated into Swift (3):

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    controller.dismiss(animated: true)
}