Does the keyboard turn on in the settings?

Is there a way to determine which keyboards are enabled in the settings?

Access to Sina Weibo is only possible if the Chinese keyboard is turned on, so I would like to show only the "Sina Weibo" button if the Chinese keyboard is available.

+4
source share
2 answers

Thanks to Guy's comment, there is a better way to do this. I 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 : Crash in iOS 9.x due to poor 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 the App Store application, but you can 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 is possible that if the user has only the default keyboard for his locale, there will be no entry for the AppleKeyboards key. In this case, you can check the user locale. If the locale is for China, then you can assume that they have a Chinese keyboard.

+4
source

This code really helps me identify whether the keyboard extension is activated or not in the device settings from the parent application 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!"); } } 
-1
source

Source: https://habr.com/ru/post/1469382/


All Articles