您可以使用以下命令获取iOS设备上安装的键盘列表:
NSUserDefaults *userDeafaults = [NSUserDefaults standardUserDefaults]; NSDictionary * userDefaultsDict = [userDeafaults dictionaryRepresentation]; NSLog(@"%@", userDefaultsDict);
这会在控制台中产生一些东西:
{ ... AppleKeyboards = ( "en_US@hw=US;sw=QWERTY", "es_ES@hw=Spanish - ISO;sw=QWERTY-Spanish", "emoji@sw=Emoji", "com.swiftkey.SwiftKeyApp.Keyboard" ); AppleKeyboardsExpanded = 1; ... }
这告诉我该设备安装了西班牙语,表情符号和SwiftKey键盘,但它没有告诉我键盘出现时将使用哪个键盘.
有办法告诉吗?
没有这方面的公共API,但是我找到了一个解决方案,它需要非常少的"灰色区域API"(如果API通常不暴露,我将API定义为"灰色区域",但可以隐藏几乎没有工作).
iOS有以下类: UITextInputMode
该类为您提供了用户可以使用的所有输入方法.仅当键盘打开时,使用以下查询将为您提供当前使用的查询:
UITextInputMode* inputMode = [[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject];
要获取扩展名(或常规Apple键盘)的显示名称,请使用:
[inputMode valueForKey:@"displayName"]
要么
[inputMode valueForKey:@"extendedDisplayName"]
这仅在键盘可见时有效.所以你必须自己监控输入模式的变化
[[NSNotificationCenter defaultCenter] addObserverForName:UITextInputCurrentInputModeDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"%@", [[[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject] valueForKey:@"extendedDisplayName"]); }); }];
我们实际上需要延迟获取当前输入模式,因为在键盘内部实现使用新值更新系统之前发送通知.在下一个runloop上获得它很有效.