2013-03-14 50 views

回答

4

感谢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; 
    } 
} 

斯威夫特:(注意:一个变通方法下的iOS 9.x的残破由于糟糕的声明UITextInputMode activeInputModesthis 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键的条目。在这种情况下,您可能想要检查用户的区域设置。如果语言环境是针对中国的,那么你应该认为他们有中文键盘。

+1

伟大的作品,谢谢。 用于检查当前语言的代码是 '如果([[[NSLocale preferredLanguages] objectAtIndex:0] hasPrefix:@ “ZH”]){// 当前语言是中国 }' – martinjbaker 2013-03-15 08:32:00

+0

该解决方案是哈克。你们应该检查出http://stackoverflow.com/questions/12816325/applekeyboards-in-ios6 – 2014-02-27 13:06:48

+0

@GuyKogus是的,这可能有点“黑客”,但它的工作原理。您链接到的问题不回答这个问题。您的链接用于确定当前键盘,而不是查看给定的键盘是否可用。 – rmaddy 2014-02-27 15:12:42

-1

这段代码是真正有用的,我来识别键盘延长被激活或者未设备设置从父应用程序本身:

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