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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

No resource found that matches the given name: attr #39;android:keyboardNavigationCluster#39;. when updating to Support Library 26.0.0(找不到与给定名称匹配的资源:attr android:keyboardNavigationCluster.更新到支持库 26.0.0 时) - IT屋-程序员软
How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How to mimic Keyboard animation on iOS 7 to add quot;Donequot; button to numeric keyboard?(如何在 iOS 7 上模拟键盘动画以添加“完成数字键盘的按钮?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)