0

I have multiple keyboards in my Xcode iOS Simulator. I'm running Swift (I need the code to work in Swift), and when they keyboard input type is changed in my app (e.g. from English to Spanish), I want to know which language it has been changed to.

I have a function in my ViewDidLoad:

NotificationCenter.default.addObserver(self,
                                               selector: #selector(changeInputMode(_:)),
                                               name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)

and this function that supports it.

@objc func changeInputMode(_ notification: Notification)
        {
            let inputMethod = UITextInputMode.activeInputModes.description
            print("keyboard changed to \(inputMethod.description)")
        }

The inputMethod description prints out this for me:

[<UIKeyboardInputMode: 0x6000027a28a0>, <UIKeyboardInputMode: 0x6000027ab2f0>, <UIKeyboardInputMode: 0x6000027bc9b0>, <UIKeyboardInputMode: 0x6000027bca50>, <UIKeyboardInputMode: 0x6000027b8ff0>, <UIKeyboardInputMode: 0x6000027b9090>]

I would like to get the language in simpler terms, such as "english" or "en" or something like that. How can this be achieved?

I also tried the following:

let language = UITextInputMode.currentInputMode()?.primaryLanguage
            print(language)

but I got an error:

'currentInputMode()' is unavailable in iOS: APIs deprecated as of iOS 7 and earlier are unavailable in Swift

Thanks for your help in advance.

Galen BlueTalon
  • 173
  • 4
  • 20
  • https://stackoverflow.com/questions/26153336/how-do-i-find-out-the-current-keyboard-used-on-ios8 Looks like its an internal API and you might not have a much better method. – ColdLogic Mar 16 '20 at 03:39
  • Is there a way to do it in swift? – Galen BlueTalon Mar 16 '20 at 03:41
  • The same APIs are typically available in swift following the same code paths. Are you asking how to do the code at the link in Swift? Or are you asking if there is a different way that is only available in Swift? – ColdLogic Mar 16 '20 at 06:17

0 Answers0