4

我已经阅读了文档,似乎找不到任何方法来检测是否在设置>常规>键盘中安装了自定义键盘?

有人知道吗?

4

2 回答 2

8

这是可能的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");
}
于 2015-07-09T17:54:49.297 回答
1

这对我有用

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
}

于 2020-08-16T10:25:45.753 回答