UITextInputMode currentInputMode is deprecated. Suggested replacement?

In our application, we want to determine the current keyboard language. For example, if a user installs several language keyboards in the "Settings" - "Basic" - "Keyboard" - "Keyboards" section, we would like to know what language they are typing in and receive 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 found with this code is that the notification works properly when the user switches the keyboard using the globe icon on the keyboard, but when we repeat UITextInputModes, they are displayed in the same order, but not (visible) which is current if we do not use the obsolete [UITextInputMode currentInputMode].

I cannot find the documentation in which Apple suggested an alternative to this deprecated functionality. There are several SO threads that mention failure, but none of them are found with solutions. Any ideas? Thanks in advance.

+4
source share
6 answers

The notification object UITextInputCurrentInputModeDidChangeNotification is an instance of UITextInputMode.

 UITextInputMode *currentInputMode = [notification object]; 

In addition, you can get an active input mode for a particular respondent:

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

Currently on iOS 7, the textInputMode / notification object is null for the emoji keyboard. It is unclear whether this is the intended behavior.

+6
source

Correct but stupid answer: use textInputMode on a UITextField, which is part of view-hirarchy.

If you (like me) to not have it (for example, because you implement inputView and don't have an official way to find the first responder), it might look like this:

 UTextField* p = [[UITextField alloc] init]; p.hidden = YES; [someView addSubview: p]; ... = p.textInputMode.primaryLanguage; [p removeFromSuperview]; 

It is so obvious and simple and requires only 3/4 Kbytes of heap. Compared to a few bytes of code in the UIKit-Library. It really makes sense ... :-)

0
source
 [UIApplication sharedApplication].delegate.window.textInputMode 

It works on ios9. Not tested on previous versions of iOS.

0
source

I do not think you should create a new UITextView or UITextField .

You already have one if your application uses a keyboard. Just get .textInputMode.primaryLanguage from an existing text control.

If you have multiple text views / fields and don’t know which one is active, you can call isFirstResponder for each. But it looks like you can get textInputMode from any text control and you don't have to find the active one.

0
source

Swift 2.0

 //richT is the TextView or textField var language = richT.textInputMode?.primaryLanguage print("language: \(language)") //UITextInputMode().primaryLanguage - returns nil value 
0
source

Update your code as follows:

 NSArray *current = [UITextInputMode activeInputModes]; UITextInputMode *current = [current firstObject]; 
-3
source

Source: https://habr.com/ru/post/1469394/


All Articles