How to detect whether custom keyboard is activated from the keyboard's container app?

前端 未结 4 1010
清酒与你
清酒与你 2020-12-01 03:13

I was wondering if there is a method that would allow me to detect from the keyboard container app whether the associated keyboard has been activated in the

4条回答
  •  无人及你
    2020-12-01 03:24

    Just in case here is Swift version of Kurt's brilliant and awesome answer:

       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
    }
    

提交回复
热议问题