1

Xcode 9.2, iOS 10.0+ , swift4.

I'm working on a project where user inputs english characters in UITextField and it converted to Japanese language characters. and it is working perfect. now, i want to allow user to input Japanese language characters direct from Japanese Keyboard.In this situation i want to know that the keyboard is changed from default to another type/language.

So, is there any function or Notification is available that can help me?

Vatsal Shukla
  • 1,274
  • 12
  • 25

5 Answers5

4

You can use the UITextInputCurrentInputModeDidChange notification to detect when the current keyboard language changes.

NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: .UITextInputCurrentInputModeDidChange, object: nil)

@objc func inputModeDidChange(_ notification: Notification) {
    if let inputMode = notification.object as? UITextInputMode {
        if let lang = inputMode.primaryLanguage {
            // do something
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • i implemented same but not getting notification. i tapped on globe icon and it shows multiple keyboard and when i changed it. it doesn't fire the notification. :( – Vatsal Shukla Aug 20 '18 at 05:37
  • i got this answer **https://stackoverflow.com/questions/46287136/uikeyboardwillchangeframe-notification-not-called-with-emoji-keyboard** . i tested with iOS10 simulator and yes it fires notification but not in iOS 11. but Any way, Thank you. – Vatsal Shukla Aug 20 '18 at 06:12
2

In newer Swift versions the notification has been renamed to UITextInputMode.currentInputModeDidChangeNotification

ph1lb4
  • 1,982
  • 17
  • 24
1

Register to receive the following notification:

UITextInputCurrentInputModeDidChangeNotification

And anytime the keyboard language changes, you'll receive a notification. You can get more info from UITextInputMode docs.

Sylvan D Ash
  • 1,047
  • 13
  • 24
1

With recent changes to iOS 12, swift 5.0 you can try:

func prepareForKeyboardChangeNotification() {
    NotificationCenter.default.addObserver(self, selector: #selector(changeInputMode), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
}

@objc
func changeInputMode(notification: NSNotification) {
    let inputMethod = txtInput.textInputMode?.primaryLanguage
    //perform your logic here

}
Amir.n3t
  • 2,859
  • 3
  • 21
  • 28
0

in swift 4.2

 //keyboard type change
    NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)


@objc func inputModeDidChange(_ notification: Notification) {
    if let inputMode = notification.object as? UITextInputMode {
        if let lang = inputMode.primaryLanguage {
            print("langueage:: \(lang)")
        }
    }
}