如何检索 Swift 中所有已安装键盘的列表?

How to retrieve the list of all installed keyboards in Swift?

我正在尝试编写一个简单的函数来检查是否安装了特定的键盘。

这是我目前在函数中的内容:

func isCustomKeyboardEnabled() {

    let bundleID:NSString = "com.company.MyApp.Keyboard"

    let installedKeyboards = NSUserDefaults.standardUserDefaults().objectForKey("AppleKeyboards")

    println(installedKeyboards)

}

这是它在控制台中 returns 的内容:

Optional((
    "en_GB@hw=British;sw=QWERTY",
    "emoji@sw=Emoji",
    "com.nuance.swype.app.Global-Keyboard",
))

我很难检查我的 bundleID 是否在这个返回的对象中。我尝试了 for inif(contains(x,x)),但都无法构建。任何帮助将非常感激。

那里有一个 Optional 响应,这意味着该值可能是 nil。尝试这样做:

if let installedKeyboards = NSUserDefaults.standardUserDefaults().objectForKey("AppleKeyboards") {
  if (contains(installedKeyboards, "Your keyboard") {
    // Do stuff.
  }
}

Swift 2.0 解法:

func installedKeyboards(){
    if let installedKeyboard = NSUserDefaults.standardUserDefaults().objectForKey("AppleKeyboards") as? [String]{
        if installedKeyboard.contains("Your Unique Identifier"){
            print("Custom Keyboard Found")
        }else{
            print("Custom Keyboard Not Installed")
        }
    }  
}

这是来自 Statik 答案的 Swift 4 版本:

func installedKeyboards() {
    if let installedKeyboard = UserDefaults.standard.object(forKey: "AppleKeyboards") as? [String] {
        if installedKeyboard.contains("Your Unique Identifier") {
            print("Custom Keyboard Found")
        }
        else {
            print("Custom Keyboard Not Installed")
        }
    }
}