1

I want to detect current input mode of keyboard, and change text direction with respect to it (rtl if Arabic, and ltr if English).

In the following code:

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

-(void)changeInputMode:(NSNotification *)notification
{
    UITextInputMode *thisInputMode = [notification object];
    NSLog(@"inputMethod=%@",thisInputMode);
}

thisInputMode is nil!

If I use this code instead:

    NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
    NSLog(@"inputMethod=%@",inputMethod);

It works fine and detects current input mode, but currentInputMode is deprecated.

Why [notification object] returns nil?

zxcat
  • 2,054
  • 3
  • 26
  • 40
Kh Jalali
  • 69
  • 7

3 Answers3

0

Use activeInputModes property instead of currentInputMode

From documentation

Each element in the array is an instance of UITextInputMode. Returns an empty array if no such instances have been set by the text input system.

Doro
  • 2,413
  • 2
  • 14
  • 26
  • You can't use it instead because `activeInputModes` returns all available input modes, not current one – zxcat Feb 20 '16 at 01:01
0

Please check this question and answers.

It have some new solutions to obtain current keyboard input mode and don't use deprecated currentInputMode method.

Community
  • 1
  • 1
zxcat
  • 2,054
  • 3
  • 26
  • 40
0

The textInputMode is not part of the notification object. The notification tells you that the language changed.

To obtain the actual information, you need to call textInputMode.primaryLanguage on your first responder (i.e. a textField) from the changeInputMode: method.

mangerlahn
  • 4,746
  • 2
  • 26
  • 50