4
A
回答
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 activeInputModes
見this 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
這段代碼是真正有用的,我來識別鍵盤延長被激活或者未設備設置從父應用程序本身:
//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. 獲取自定義鍵盤中isSticky鍵的當前狀態
- 2. 獲取Vim中設置的當前值
- 3. 獲取當前的鍵盤佈局的語言在JavaScript
- 4. 當viewDidLoad設置textField可編輯/啓用鍵盤
- 5. 獲取ICU的當前區域設置
- 6. 獲取當前活動的鍵盤語言作爲NSLocale
- 7. iOS:如何獲取當前可見的鍵盤類型?
- 8. 如何獲取當前鍵盤佈局的代碼頁?
- 9. 獲取當前PHP安裝設置
- 10. 獲取當前默認maxConcurrentSessions設置
- 11. 無法獲取當前設備位置
- 12. 如何在應用程序啓動時獲取當前位置?
- 13. 獲取當前在C++中設置的全局語言環境?
- 14. 如何獲取在Unix中更改當前鍵盤佈局的事件?
- 15. 從gps啓用設備獲取當前的經緯度
- 16. 在GPS中獲取當前位置?
- 17. 在mysql表中獲取當前位置
- 18. iOS8鍵盤擴展獲取當前上下文
- 19. 軟鍵盤設置
- 20. 如何在Xamarin中獲取/設置當前音量水平?
- 21. 在鍵盤上獲取MotionEvent
- 22. 在設置包中關閉鍵盤/數字鍵盤iOS
- 23. 在Android自定義鍵盤中設置鍵盤模式
- 24. Android獲取當前位置
- 25. android獲取當前位置
- 26. 獲取當前位置android
- 27. 獲取當前位置
- 28. 獲取當前位置
- 29. 獲取當前位置
- 30. 打開鍵盤時獲取鍵盤高度或頂部位置
偉大的作品,謝謝。 用於檢查當前語言的代碼是 '如果([[[NSLocale preferredLanguages] objectAtIndex:0] hasPrefix:@ 「ZH」]){// 當前語言是中國 }' – martinjbaker 2013-03-15 08:32:00
該解決方案是哈克。你們應該檢查出http://stackoverflow.com/questions/12816325/applekeyboards-in-ios6 – 2014-02-27 13:06:48
@GuyKogus是的,這可能有點「黑客」,但它的工作原理。您鏈接到的問題不回答這個問題。您的鏈接用於確定當前鍵盤,而不是查看給定的鍵盤是否可用。 – rmaddy 2014-02-27 15:12:42