MFMailComposeViewController Keyboard Issue

1.9k views Asked by At

How do i dismiss the keyboard without pressing the Send or Cancel button in MFMailComposeViewController?!

Thanks for any help.

3

There are 3 answers

1
visakh7 On BEST ANSWER

Can you try this.

UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:@selector(firstResponder)];
[firstResponder resignFirstResponder];

hope this helps....

2
Lukas Gross On

I experienced a similar problem: For some reason iOS does not dismiss the Keyboard of a MFMailComposeViewController when the application enters background (the dismiss happens when the application becomes active again). However iOS dismisses the keyboard if the first responder is a simple element (e.g. textview). Calling resignFirstResponder did not work for me in this particular case. Because I switch windows on applicationBecomeActive (to show a login screen) I ended up having multiple keyboards above each other (the one on the top not working). I found a simple workaround to dismiss the keyboard of an MFMailComposeViewController when the application resigns active:

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Workaround: MFMailComposeViewController does not dismiss keyboard when application enters background
    UITextView *dummyTextView = [[UITextView alloc] init];
    [self.window.rootViewController.presentedViewController.view addSubview:dummyTextView];
    [dummyTextView becomeFirstResponder];
    [dummyTextView resignFirstResponder];
    [dummyTextView removeFromSuperview];
    // End of workaround
}

This will implicitly resign the first responder if we have any viewController that is currently beeing presented.

0
Anomie On

While you probably can do it by finding whichever view is the first responder and calling resignFirstResponder on it (unless you're on iPad and MFMailComposeViewController uses UIModalPresentationFormSheet), Apple might reject your app for it. Quoth the documentation:

Important: The mail composition interface itself is not customizable and must not be modified by your application.

This could easily be construed to include the behavior of the keyboard.