2013-03-14 51 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!"); 
      } 
     }