【问题标题】:Detect if Custom Keyboard has been installed检测是否已安装自定义键盘
【发布时间】:2014-08-16 21:34:08
【问题描述】:

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

有人知道吗?

【问题讨论】:

  • @CrimsonChris 即使是这样,也没有证据表明我曾经签署过保密协议,我可能只是一般性地问......而回答的人也可能只是用他们的知识回答从未与 Apple 达成 NDA。 :)
  • 不能再同意了。当问题被标记时,我一直在烦恼,因为有问题的平台仍处于测试阶段。这个问题可能适合赏金。
  • 为什么需要知道是否安装了自定义键盘?你确定你不只是需要它的高度或其他东西吗?
  • 你可以强制键盘显示没有用户交互。然后立即关闭它。

标签: ios objective-c ios8 ios-app-extension custom-keyboard


【解决方案1】:

NSUserDefaults 可以做到这一点。只需检索包含用户为键“AppleKeyboards”安装的所有键盘的数组的standardUserDefaults 对象。然后检查该数组是否包含您的键盘扩展的捆绑标识符。

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");
}

【讨论】:

    【解决方案2】:

    这对我有用

    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
    }

    【讨论】:

      猜你喜欢
      • 2019-10-29
      • 2014-07-12
      • 1970-01-01
      • 2015-11-18
      • 2014-10-29
      • 2013-06-18
      • 2016-12-10
      • 1970-01-01
      相关资源
      最近更新 更多