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

How do I find out the current keyboard used on iOS8?(如何找出当前在 iOS8 上使用的键盘?)

本文介绍了如何找出当前在 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 上使用的键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何找出当前在 iOS8 上使用的键盘?

基础教程推荐