Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out the current keyboard used on iOS8?

Tags:

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?

like image 406
awolf Avatar asked Oct 02 '14 01:10

awolf


People also ask

Can I change the keyboard on my iPhone?

Go to Settings > General > Keyboard. Tap Keyboards, then do any of the following: Add a keyboard: Tap Add New Keyboard, then choose a keyboard from the list. Repeat to add more keyboards.

How do I get the full keyboard on my iPhone?

Go to Settings > Accessibility > Keyboards, tap Full Keyboard Access, then turn on Full Keyboard Access.

What does allow full access to keyboard mean?

Last Updated: 241d. "Full access" allows the Disney Emoji Blitz keyboard to add Disney emojis to your messages. Disney Emoji Blitz does not and cannot track keystrokes for any other keyboards and cannot access any private information stored on your device.


2 Answers

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.

like image 146
Léo Natan Avatar answered Oct 05 '22 07:10

Léo Natan


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...

like image 20
benface Avatar answered Oct 05 '22 07:10

benface