【问题标题】:Get keyboards currently enabled in Settings?获取当前在“设置”中启用的键盘?
【发布时间】:2013-03-03 13:53:36
【问题描述】:

有什么方法可以识别在“设置”中启用了哪些键盘?

只有在启用了中文键盘的情况下才能分享到新浪微博,所以我想只在有中文键盘的情况下显示“新浪微博”操作表按钮。

【问题讨论】:

    标签: ios cocoa-touch social-networking weibo sinaweibo


    【解决方案1】:

    感谢 Guy 的评论,有更好的方法可以做到这一点。我已经更新了自己的代码以使用以下代码:

    NSArray *keyboards = [UITextInputMode activeInputModes];
    for (UITextInputMode *mode in keyboards) {
        NSString *name = mode.primaryLanguage;
        if ([name hasPrefix:@"zh-Han"]) {
            // One of the Chinese keyboards is installed
            break;
        }
    }
    

    Swift:(注意:由于 UITextInputMode activeInputModes 的声明错误,在 iOS 9.x 下损坏。有关解决方法,请参阅 this answer。)

    let keyboards = UITextInputMode.activeInputModes()
    for var mode in keyboards  {
        var primary = mode.primaryLanguage
        if let lang = primary {
            if lang.hasPrefix("zh") {
                // One of the Chinese keyboards is installed
                break
            }
        }
    }
    

    旧方法:

    我不知道 App Store 应用中是否允许这样做,但您可以这样做:

    NSArray *keyboards = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleKeyboards"];
    for (NSString *keyboard in keyboards) {
        if ([keyboard hasPrefix:@"zh_Han"]) {
            // One of the Chinese keyboards is installed
        }
    }
    

    如果用户的语言环境只有一个默认键盘,则可能没有AppleKeyboards 键的条目。在这种情况下,您可能需要检查用户的语言环境。如果语言环境是中国,那么您应该假设他们有中文键盘。

    【讨论】:

    • 效果很好,谢谢。检查当前语言的代码是if ([[[NSLocale preferredLanguages] objectAtIndex:0] hasPrefix:@"zh"]) { // current language is Chinese }
    • 这个解决方案很老套。你们应该看看stackoverflow.com/questions/12816325/applekeyboards-in-ios6
    • @GuyKogus 是的,这可能有点“hacky”,但它确实有效。您链接到的问题不回答这个问题。您的链接用于确定当前键盘,而不是查看给定键盘是否可用。
    • 它适用于获取可用的键盘。我在 iOS 6 和 7 上都试过了。
    • @GuyKogus 看来我误读了该课程的作用。我刚刚试了一下,果然使用UITextInputMode activeInputModes 确实反映了选定的键盘。我已经更新了我的答案以反映这一点。谢谢。
    【解决方案2】:

    这段代码对我从父应用程序本身的设备设置中识别键盘扩展是否激活很有帮助:

        //Put below function in app delegate...
    
        public func isKeyboardExtensionEnabled() -> Bool {
            guard let appBundleIdentifier = NSBundle.mainBundle().bundleIdentifier else {
                fatalError("isKeyboardExtensionEnabled(): Cannot retrieve bundle identifier.")
            }
    
            guard let keyboards = NSUserDefaults.standardUserDefaults().dictionaryRepresentation()["AppleKeyboards"] as? [String] else {
                // There is no key `AppleKeyboards` in NSUserDefaults. That happens sometimes.
                return false
            }
    
            let keyboardExtensionBundleIdentifierPrefix = appBundleIdentifier + "."
            for keyboard in keyboards {
                if keyboard.hasPrefix(keyboardExtensionBundleIdentifierPrefix) {
                    return true
                }
            }
    
            return false
        }
    
    
        // Call it from below delegate method to identify...
    
        func applicationWillEnterForeground(_ application: UIApplication) {
    
                if(isKeyboardExtensionEnabled()){            
                    showAlert(message: "Hurrey! My-Keyboard is activated");
                }
                else{
                    showAlert(message: "Please activate My-Keyboard!");
                }
    
            }
    
        func applicationDidBecomeActive(_ application: UIApplication) {
    
                if(isKeyboardExtensionEnabled()){            
                    showAlert(message: "Hurrey! My-Keyboard is activated");
                }
                else{
                    showAlert(message: "Please activate My-Keyboard!");
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多