我已经阅读了文档,似乎找不到任何方法来检测是否在设置>常规>键盘中安装了自定义键盘?
有人知道吗?
我已经阅读了文档,似乎找不到任何方法来检测是否在设置>常规>键盘中安装了自定义键盘?
有人知道吗?
这是可能的NSUserDefaults
。只需检索standardUserDefaults
包含用户为键“AppleKeyboards”安装的所有键盘的数组的对象。然后检查该数组是否包含您的键盘扩展的捆绑标识符。
NSArray *keyboards = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleKeyboards"];
NSLog(@"keyboards: %@", keyboards);
// check for your keyboard
NSUInteger index = [keyboards indexOfObject:@"com.example.productname.keyboard-extension"];
if (index != NSNotFound) {
NSLog(@"found keyboard");
}
这对我有用
func isKeyboardExtensionEnabled() -> Bool {
guard let appBundleIdentifier = Bundle.main.bundleIdentifier else {
fatalError("isKeyboardExtensionEnabled(): Cannot retrieve bundle identifier.")
}
guard let keyboards = UserDefaults.standard.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
}