如何找到iOS8上使用的当前键盘? [英] How do I find out the current keyboard used on iOS8?

查看:278
本文介绍了如何找到iOS8上使用的当前键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

You can get a list of the keyboards installed on the iOS device using:

NSUserDefaults *userDeafaults = [NSUserDefaults standardUserDefaults];
NSDictionary * userDefaultsDict = [userDeafaults dictionaryRepresentation];
NSLog(@"%@", userDefaultsDict);

这会在控制台中产生如下内容:

This yields something in the console like:

{
    ...
    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键盘,但它没有告诉我键盘出现时会使用哪个键盘。

This tells me that the device has the Spanish, Emoji and SwiftKey keyboards installed, but it tells me nothing about which will be used when the keyboard comes up.

有没有办法告诉?

推荐答案

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

There is no public API for this, but I found a solution for you, which requires very little "gray area API" (I define API as "gray area" if an API is not normally exposed, but can be hidden with little to no work).

iOS具有以下类: UITextInputMode

iOS has the following class: UITextInputMode

此类为您提供用户可以使用的所有输入方法。使用以下查询将为您提供当前使用的仅在键盘打开时

This class gives you all the input methods the user can use. Using the following query will give you the currently used, only when the keyboard is open:

UITextInputMode* inputMode = [[[UITextInputMode activeInputModes] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isDisplayed = YES"]] lastObject];

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

To get the display name of the extension (or regular Apple keyboard), use:

[inputMode valueForKey:@"displayName"]

[inputMode valueForKey:@"extendedDisplayName"]

仅当键盘可见时才有效。所以你必须自己监视输入模式的变化

This only works when the keyboard is visible. So you will have to monitor input mode change yourself using

[[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上获得它很有效。

We actually need to delay obtaining the current input mode, as the notification is sent before the keyboard internal implementation has updated the system with new value. Obtaining it on the next runloop works well.

这篇关于如何找到iOS8上使用的当前键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆