4

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.

martinjbaker
  • 1,424
  • 15
  • 23

2 Answers2

4

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.

Community
  • 1
  • 1
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Works great, thanks. The code for checking current language is `if ([[[NSLocale preferredLanguages] objectAtIndex:0] hasPrefix:@"zh"]) { // current language is Chinese }` – martinjbaker Mar 15 '13 at 08:32
  • This solution is hacky. You guys should check out http://stackoverflow.com/questions/12816325/applekeyboards-in-ios6 – Guy Kogus Feb 27 '14 at 13:06
  • @GuyKogus Yes, this may be a little "hacky" but it works. The question you link to does not answer this question. Your link is for determining the current keyboard, not for seeing if a given keyboard is available. – rmaddy Feb 27 '14 at 15:12
  • It works for getting the available keyboards. I've tried this on both iOS 6 & 7. – Guy Kogus Feb 27 '14 at 15:31
  • 1
    @GuyKogus It seems I misread what that class does. I just tried it out and sure enough using `UITextInputMode activeInputModes` does reflect the selected keyboards. I've updated my answer to reflect this. Thanks. – rmaddy Feb 27 '14 at 15:49
-1

This code is really helpful for me to identify keyboard extension is activated or not in device settings from parent app itself:

    //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!");
            }
        }
Sandip Patel - SM
  • 3,346
  • 29
  • 27