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.
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.
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.
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!");
}
}