MFMailComposeViewController Keyboard Issue
Asked Answered
S

3

10

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

Thanks for any help.

Skyscraper answered 2/2, 2011 at 9:16 Comment(2)
Out of curiosity, why would you want to?Louislouisa
My App was rejected today due to using the code suggested by 7KV7. Just so you know not to use this in apps on the app store.Jaw
M
5

Can you try this.

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

hope this helps....

Mangan answered 8/6, 2011 at 5:20 Comment(1)
Note that UIWindow's firstResponder method is a private API, so using it is liable to get Apple to reject your app if you submit it to the App Store and liable to stop working without notice in future versions if iOS.Suttles
H
5

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.

Hearken answered 20/6, 2013 at 7:22 Comment(2)
Thx! It seems that it's the only valid way to dismiss keyboard for MFMailComposeViewController (or SLComposeViewController) due to introduction of remote views in iOS >= 6 (_UIRemoteView)Cattycornered
You are absolutely right - thank you for pointing that out. To clarify this: My assumption is that as remote views run in a separate process you cannot take them the keyboard away, however by requesting first responder for an app-controlled view you implicitly take over control of the keyboard again.Hearken
S
2

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.

Suttles answered 8/6, 2011 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.