1

I already found a lot of approaches for this but no working solution. Here's what I tried and didn't work.

(1) Simply calling primaryLanguage()

UITextInputMode().primaryLanguage

→ always returns nil :-/

(2) Subscribing to UITextInputCurrentInputModeDidChangeNotification notifications

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeInputMode:", name: UITextInputCurrentInputModeDidChangeNotification, object: nil)
}

func changeInputMode(sender : NSNotification) {
    ...?!
}

The notification is getting triggered but it is unclear how I can extract the current language information from the notification.

(3) Using activeInputModes()

let localeIdentifier = UITextInputMode.activeInputModes().first as? UITextInputMode
var locale:NSLocale = NSLocale(localeIdentifier: localeIdentifier.primaryLanguage!)
println("Input Mode Language \(localeIdentifier.primaryLanguage!)")

This always provides the same array of all available keyboards, no information on the actually active one.

How do I get the NSLocale of the currently active keyboard?

4

1 回答 1

9

您可以通过像这样访问文本字段来从每个文本字段访问primaryLanguage textInputMode

var language = textfield.textInputMode?.primaryLanguage
于 2015-02-23T20:26:14.867 回答