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

 胡龙云积极_622 发布于 2022-12-18 17:50

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

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

这会在控制台中产生一些东西:

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

有办法告诉吗?

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

    iOS有以下类: UITextInputMode

    该类为您提供了用户可以使用的所有输入方法.仅当键盘打开时,使用以下查询将为您提供当前使用的查询:

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

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

    [inputMode valueForKey:@"displayName"]
    

    要么

    [inputMode valueForKey:@"extendedDisplayName"]
    

    这仅在键盘可见时有效.所以你必须自己监控输入模式的变化

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

    2022-12-18 17:53 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有