在我们的应用程序中,我们想要检测当前的键盘语言.例如,如果用户在"设置" - >"常规" - >"键盘" - >"键盘"下设置多个语言键盘,我们想知道他们输入的语言,并在此更改时从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);
}
Run Code Online (Sandbox Code Playgroud)
我们用这段代码发现的是当用户使用键盘上的地球图标切换键盘时,通知会正确触发,但是当我们迭代UITextInputModes时,它们以相同的顺序出现,没有(明显)指示哪个是当前的一个,除非我们使用现已弃用的[UITextInputMode currentInputMode].
我找不到任何文档表明Apple建议替代现已弃用的功能.有几个SO线程提到了弃用,但我找不到解决方案.有任何想法吗?提前致谢.