Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Custom Keyboard has been installed

I've read through the documentation and can't seem to find any method of how can I detect if a custom keyboard has been installed in settings>general>keyboards?

Does anyone know of any?

like image 560
Albert Renshaw Avatar asked Jan 10 '23 00:01

Albert Renshaw


2 Answers

This is possible with NSUserDefaults. Just retrieve the standardUserDefaults object which contains an array of all the keyboards the user has installed for key "AppleKeyboards". Then check if the array contains the bundle identifier for your keyboard extension.

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");
}
like image 153
Matt Avatar answered Jan 18 '23 17:01

Matt


This works for me

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
}
like image 43
harryngh Avatar answered Jan 18 '23 17:01

harryngh