iPad - Dismiss keyboard for modal view controller

2019-08-22 09:54发布

问题:

In my iPad app I want to present some view controllers in UIModalPresentationFormSheet modal mode without keyboard.

I use it to display help as an example.

At the moment I use the code found on the one of stackoverflow answers to dismiss it:

// trick to dismiss keyboard in iPad:    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        // iPad specific behaviour:

        @try
        {
            Class UIKeyboardImpl = NSClassFromString(@"UIKeyboardImpl");
            id activeInstance = [UIKeyboardImpl performSelector:@selector(activeInstance)];
            [activeInstance performSelector:@selector(dismissKeyboard)];
        }
        @catch (NSException *exception)
        {
            //NSLog(@"%@", exception);
        }

    }

But I am afraid Apple can reject it in during approval process as it uses private API

I can see Apple developers achieved that in the GarageBand help screens so it must be the 'proper' way to do this.

Would appreciate help as our client do not want to change design concept because of such a slight limitation.

UPDATE: Just today I was rejected from AppStore:

We found that your app uses one or more non-public APIs, which is not in compliance with the App Store Review Guidelines. The use of non-public APIs is not permissible because it can lead to a poor user experience should these APIs change.

We found the following non-public APIs in your app:

activeInstance dismissKeyboard

So please do NOT follow this advice: How to HIDE the iPad keyboard from a MODAL view controller?

回答1:

As I said in a comment in the mentioned question: you can construct the selector dynamically with NSSelectorFromString(). This will be accepted for the AppStore, your bug will be fixed and your code will not crash.