1

I've got a standard implementation of the MFMailComposeViewController.

I've set the right delegate protocols, and have a log happening on the didFinishWithResult method.

See:

mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setSubject:emailTitle];
[mailComposer setMessageBody:messageBody isHTML:YES];
mailComposer.mailComposeDelegate = self;

[[self getController] presentViewController:mailComposer animated:YES completion:NULL];

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    NSLog(@"mail dismiss");
    [[self getController] dismissViewControllerAnimated:YES completion:NULL];
}

That shows the mail composer properly, and everything works well. Meaning, if I press the "cancel" button, the didFinishWithResult method gets called and the mailComposer gets dismissed.

However, if I try to type anything, such as TO: email address, or anything else in the mail composer itself, it feels like the keyboard appearing is removing the delegate actions of my view controller, since the "cancel" and "send" buttons trigger no actions.

Any thoughts? It's driving me crazy :/

Cheers

EDIT

Here's the code for getController:

- (UIViewController *) getController
{
    Class vcc = [UIViewController class];

    UIResponder *responder = self;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: vcc])
            return (UIViewController *)responder;

    return nil;
}
Andre
  • 4,417
  • 8
  • 32
  • 37

3 Answers3

2

When UITextField becomeFirstResponder, than your controller recieved resignFirstResponder.

Why are you using method "getController"? Create property on ViewController which present MFMailComposeViewController.

Misha Vyrko
  • 992
  • 6
  • 14
  • Whilst this feels like a throwaway answer, it was the easy solution to go for. I was subclassing a view controller already, so I just added a "send email" method to it, which handles the present/dismiss of the email. Thanks – Andre Mar 05 '15 at 18:37
0

Set up a switch to catch the results form the mail composer like so:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled:
            // User tapped cancel button
            break;
        case MFMailComposeResultSaved:
            // User saved email
            break;
        case MFMailComposeResultSent:
            // User sent email
            break;
        case MFMailComposeResultFailed:
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
  • Hi Daniel. I had that before and it didn't help. I don't see how that could make a difference, considering the "dismiss" is not related to the result. :/ – Andre Mar 02 '15 at 09:38
0

I'd recommend this implementation:

- (void) presentMailViewController
    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mail = [MFMailComposeViewController new];
        mail.mailComposeDelegate = self;
        mail.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;;
        [self.navigationController presentViewController:mail animated:YES completion:nil];
    }   
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    if(error)
    {
       //...
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}   

And I'd recommend you to read this page: Code Naming Basics

Apoc
  • 797
  • 1
  • 10
  • 15