在我们的应用中,我们希望检测当前键盘的语言。例如,如果用户在“设置”->“通用”->“键盘”->“键盘”下设置了多种语言键盘,我们想知道他们正在输入的语言,并在此更改时从NSNotificationCenter接收通知。
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *nCenter = [NSNotificationCenter defaultCenter];
[nCenter addObserver:self selector:@selector(languageChanged:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
[self languageChanged:nil];
}
-(void)languageChanged:(NSNotification*)notification
{
for(UITextInputMode *mode in [UITextInputMode activeInputModes])
{
NSLog(@"Input mode: %@", mode);
NSLog(@"Input language: %@", mode.primaryLanguage);
}
NSLog(@"Notification: %@", notification);
UITextInputMode *current = [UITextInputMode currentInputMode];
NSLog(@"Current: %@", current.primaryLanguage);
}
我们在这段代码中发现的是,当用户使用键盘上的地球标志切换键盘时,通知可以正确触发,但是当我们遍历UITextInputModes时,它们以相同的顺序出现,没有(明显的)指示哪个是当前模式,除非我们使用现在已被弃用的[UITextInputMode currentInputMode]。
我找不到任何说明苹果建议替代此现在已废弃功能的文档。有几个SO线程提到了弃用,但我没有找到解决方案。有什么想法吗?先谢谢了。