Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextInputMode currentInputMode is deprecated. Suggested replacement?

In our app, we'd like to detect the current keyboard language. For example, if the user sets up multiple language keyboards under Settings->General->Keyboard->Keyboards, we'd like to know what language they're typing in and to get a notification from NSNotificationCenter when this changes.

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSNotificationCenter *nCenter = [NSNotificationCenter defaultCenter];
    [nCenter addObserver:self selector:@selector(languageChanged:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
    [self languageChanged:nil];
}

-(void)languageChanged:(NSNotification*)notification
{
    for(UITextInputMode *mode in [UITextInputMode activeInputModes])
    {
        NSLog(@"Input mode: %@", mode);
        NSLog(@"Input language: %@", mode.primaryLanguage);
    }
    NSLog(@"Notification: %@", notification);
    UITextInputMode *current = [UITextInputMode currentInputMode];
    NSLog(@"Current: %@", current.primaryLanguage);
}

What we've discovered with this code is that the notification fires appropriately when the user toggles keyboards using the globe icon on the keyboard, but when we iterate the UITextInputModes, they appear in the same order with no (apparent) indication of which is the current one unless we use the now-deprecated [UITextInputMode currentInputMode].

I can't find any documentation indicating Apple's suggested alternative to this now-deprecated functionality. There are several SO threads mentioning the deprecation, but none that I found with solutions. Any ideas? Thanks in advance.

like image 426
Matt Burnside Avatar asked Mar 24 '14 16:03

Matt Burnside


1 Answers

The object for the notification UITextInputCurrentInputModeDidChangeNotification is an instance of UITextInputMode.

UITextInputMode *currentInputMode = [notification object];

Also, you can get the active input mode on a particular responder:

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

Currently on iOS 7, the textInputMode/notification object are nil for the emoji keyboard. It's unclear if this is intended behaviour.

like image 89
jeyb Avatar answered Sep 20 '22 13:09

jeyb