20

You can get a list of the keyboards installed on the iOS device using:

NSUserDefaults *userDeafaults = [NSUserDefaults standardUserDefaults];
NSDictionary * userDefaultsDict = [userDeafaults dictionaryRepresentation];
NSLog(@"%@", userDefaultsDict);

This yields something in the console like:

{
    ...
    AppleKeyboards =     (
        "en_US@hw=US;sw=QWERTY",
        "es_ES@hw=Spanish - ISO;sw=QWERTY-Spanish",
        "emoji@sw=Emoji",
        "com.swiftkey.SwiftKeyApp.Keyboard"
    );
    AppleKeyboardsExpanded = 1;
    ...
}

This tells me that the device has the Spanish, Emoji and SwiftKey keyboards installed, but it tells me nothing about which will be used when the keyboard comes up.

Is there a way to tell?

7
  • Do you need keyboard or language? Language is possible, but which keyboard exactly is not possible using public API.
    – Léo Natan
    Oct 7, 2014 at 21:55
  • @LeoNatan which keyboard.
    – awolf
    Oct 8, 2014 at 16:43
  • Are you interested in gray area or private API solutions?
    – Léo Natan
    Oct 8, 2014 at 16:43
  • Given that the keyboards, once activated, can be cycled through at the user's whim, I'm not sure answering your question "which will be used when the keyboard comes up?" is valid. Can you share what are you trying to accomplish? Oct 14, 2014 at 16:49
  • 1
    @LeoNatan I'd prefer a legitimate, fully supported method. But it's looking like that's not possible. In lieu of that, any option is better than none I suppose.
    – awolf
    Oct 14, 2014 at 17:30

5 Answers 5

18
+50

There is no public API for this, but I found a solution for you, which requires very little "gray area API" (I define API as "gray area" if an API is not normally exposed, but can be hidden with little to no work).

iOS has the following class: UITextInputMode

This class gives you all the input methods the user can use. Using the following query will give you the currently used, only when the keyboard is open:

UITextInputMode* inputMode = [[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject];

To get the display name of the extension (or regular Apple keyboard), use:

[inputMode valueForKey:@"displayName"]

or

[inputMode valueForKey:@"extendedDisplayName"]

This only works when the keyboard is visible. So you will have to monitor input mode change yourself using

[[NSNotificationCenter defaultCenter] addObserverForName:UITextInputCurrentInputModeDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
 {
     dispatch_async(dispatch_get_main_queue(), ^{
         NSLog(@"%@", [[[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject] valueForKey:@"extendedDisplayName"]);
     });
 }];

We actually need to delay obtaining the current input mode, as the notification is sent before the keyboard internal implementation has updated the system with new value. Obtaining it on the next runloop works well.

2
  • 2
    Thanks Leo, you nailed it. This is what I'll be using in the next release of my app. And, I'd definitely say this is 100% kosher w.r.t. to app store guidelines.
    – awolf
    Oct 14, 2014 at 23:23
  • 1
    A note here: the "isDisplayed" property only exists on UITextInputMode in iOS 8 and greater, so for apps that support earlier OS versions it's necessary to iterate the activeInputModes and test if they respondToSelector @selector(isDisplayed) (or, avoid running this code for the earlier OSes).
    – awolf
    Nov 1, 2014 at 21:00
4

Leo Natan's answer is great, but I would like to add something to it. You can actually get the current input mode at any time, not just when the keyboard is open, like this:

UITextView *textView = [[UITextView alloc] init];
UITextInputMode *inputMode = textView.textInputMode;

Please note that textView.textInputMode is nil for the Emoji keyboard for some reason.

Also, in addition to displayName and extendedDisplayName, there are other keys you can retrieve, such as identifier, normalizedIdentifier (iOS 8+), hardwareLayout, ... See the full API here:

https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIKeyboardInputMode.h

Now I'm not sure if using any of those is more risky than displayName for App Store approval...

1

it works for me Swift 5.0

 NotificationCenter.default.addObserver(self, selector: #selector(keyBoardChanged(_:)), name:UITextInputMode.currentInputModeDidChangeNotification, object: nil)

  @objc func keyBoardChanged(_ notification: NSNotification){
    if let identifier = textField.textInputMode?.perform(NSSelectorFromString("identifier"))?.takeUnretainedValue() as? String{
        if identifier == "YOUR APP IDENTIFIER"{
            //Do Whatever you required :) 
        }
    }
 }
0

@Leo Natan's answers is cool but it's may return nil when the keyboard have not display.

So here I use the string to find the UIKeyboardInputMode's property.

I can tell you that this can find out the current keyboard because it's comes from Apple's Private API.

Code here:

+ (BOOL)isTheCustomKeyboard
{
    UITextInputMode* inputMode = [UITextInputMode currentInputMode];
    if ([inputMode respondsToSelector:NSSelectorFromString(@"identifier")])
    {
        NSString* indentifier = [inputMode performSelector:NSSelectorFromString(@"identifier")];
        if ([indentifier isEqualToString: YOUR_APP_ID])
        {
            return YES;
        }
    }
   return NO;
}

And more:

+ (BOOL)isContaintCustomKeyboard
{
    NSArray * inputModes = [UITextInputMode activeInputModes];
    for (id inputModel in inputModes)
    {
        if ([inputModel respondsToSelector:NSSelectorFromString(@"identifier")])
        {
            NSString* indentifier = [inputModel performSelector:NSSelectorFromString(@"identifier")];
            if ([indentifier isEqualToString: YOUR_APP_ID])
            {
                return YES;
            }
        }
    }
    return NO;
}

Actually we can also use the displayName or the identifier and more.

0

Swift 5

let inputMode = UIApplication.shared.delegate?.window??.textInputMode
if inputMode?.responds(to: NSSelectorFromString("identifier")) ?? false {
     let identifier = inputMode?.perform(NSSelectorFromString("identifier")).takeRetainedValue() as? String
     print("\(identifier) // Current keyboard identifier.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.