iosswiftuikeyboard

iOS: How to detect keyboard change event


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?


Solution

  • 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
            }
        }
    }