如何找到iOS8上使用的当前键盘?

awo*_*olf 18 keyboard nsuserdefaults ios ios8

您可以使用以下命令获取iOS设备上安装的键盘列表:

NSUserDefaults *userDeafaults = [NSUserDefaults standardUserDefaults];
NSDictionary * userDefaultsDict = [userDeafaults dictionaryRepresentation];
NSLog(@"%@", userDefaultsDict);
Run Code Online (Sandbox Code Playgroud)

这会在控制台中产生一些东西:

{
    ...
    AppleKeyboards =     (
        "en_US@hw=US;sw=QWERTY",
        "es_ES@hw=Spanish - ISO;sw=QWERTY-Spanish",
        "emoji@sw=Emoji",
        "com.swiftkey.SwiftKeyApp.Keyboard"
    );
    AppleKeyboardsExpanded = 1;
    ...
}
Run Code Online (Sandbox Code Playgroud)

这告诉我该设备安装了西班牙语,表情符号和SwiftKey键盘,但它没有告诉我键盘出现时将使用哪个键盘.

有办法告诉吗?

Leo*_*ica 16

没有这方面的公共API,但是我找到了一个解决方案,它需要非常少的"灰色区域API"(如果API通常不暴露,我将API定义为"灰色区域",但可以隐藏几乎没有工作).

iOS有以下类: UITextInputMode

该类为您提供了用户可以使用的所有输入方法.仅当键盘打开时,使用以下查询将为您提供当前使用的查询:

UITextInputMode* inputMode = [[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject];
Run Code Online (Sandbox Code Playgroud)

要获取扩展名(或常规Apple键盘)的显示名称,请使用:

[inputMode valueForKey:@"displayName"]
Run Code Online (Sandbox Code Playgroud)

要么

[inputMode valueForKey:@"extendedDisplayName"]
Run Code Online (Sandbox Code Playgroud)

这仅在键盘可见时有效.所以你必须自己监控输入模式的变化

[[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"]);
     });
 }];
Run Code Online (Sandbox Code Playgroud)

我们实际上需要延迟获取当前输入模式,因为在键盘内部实现使用新值更新系统之前发送通知.在下一个runloop上获得它很有效.

  • 谢谢狮子座,你钉了它.这是我将在我的应用程序的下一个版本中使用的内容.而且,我肯定会说这是应用商店指南的100%kosher wrt. (2认同)