Search code examples
ioscocoa-touchsocial-networkingweibosinaweibo

Get keyboards currently enabled in Settings?


Is there any way to identify which keyboards are enabled in Settings?

Sharing to Sina Weibo is only possible if a Chinese keyboard is enabled, so I'd like to only show the "Sina Weibo" action sheet button if a Chinese keyboard is available.


Solution

  • Thanks to the comment by Guy, there is a better way to do this. I've updated my own code to use the following:

    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: (Note: Broken under iOS 9.x due to a bad declaration for UITextInputMode activeInputModes. See this answer for a workaround.)

    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
            }
        }
    }
    

    Old approach:

    I don't know if this is allowed or not in an App Store app, but you could do:

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

    It's possible that if the user only has a default keyboard for their locale, there won't be an entry for the AppleKeyboards key. In this case, you may want to check the user's locale. If the locale is for China, then you chould assume they have a Chinese keyboard.