Search code examples
iosobjective-cmfmailcomposer

MFMailComposeViewController didFinishWithResult issue when the keyboard appears


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;
}

Solution

  • When UITextField becomeFirstResponder, than your controller recieved resignFirstResponder.

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