How do I find out the current keyboard used on 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 上使用的键盘?
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01