如何判断用户正在使用或切换到哪个第三方键盘?
我没有找到任何可用于检查此信息的通知或 API。
您无法知道安装了哪些键盘,也无法决定接下来要加载哪个键盘。
苹果的文件指出:
要要求系统切换到另一个键盘,请调用 前进到下一个输入模式方法 (..(系统选择适当的 "下一个"键盘;没有用于获取已启用键盘列表的 API 或用于选择要切换到的特定键盘。
有关更多信息,请阅读 Apple 的自定义键盘 API 文档:https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html#//apple_ref/doc/uid/TP40014214-CH16-SW21
有点晚了,但您可以在设备上获取活动键盘的列表。所有键盘标识符都存储在UserDefaults
中,以下是获取它们的方法:
/**
- Author:
Panayot Panayotov
- returns:
True or False
- Important:
Result is returned immediately after non system identifier is found
This method iterates through all public keyboard identifiers and
evaluates a regex that matches system keyboards
[
"nb_NO@sw=QWERTY-Norwegian;hw=Automatic",
"zh_Hant-Zhuyin@sw=Zhuyin;hw=Automatic",
"ur@sw=Urdu;hw=Automatic",
"zh_Hant-HWR@sw=HWR-Traditional",
"zh_Hant-Pinyin@sw=Pinyin10-Traditional;hw=Automatic",
"emoji@sw=Emoji"
]
*/
-(BOOL)hasUnkownKeyboard {
NSString * appleKeyboardsRegexp = @"^(?:[0-9a-zA-Z_\-]+)(?:@sw|@hw)[0-9a-zA-Z_\-\-;=]*$";
NSPredicate * appleKeyboardsTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", appleKeyboardsRegexp];
NSArray *keyboards = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] objectForKey:@"AppleKeyboards"];
for (NSString *keyboard in keyboards) {
if(![appleKeyboardsTest evaluateWithObject:keyboard]){
return YES;
}
}
return NO;
}