我正在尝试编写一个简单的函数来检查是否安装了特定的键盘。
到目前为止,这是我在函数中的内容:
func isCustomKeyboardEnabled() {
let bundleID:NSString = "com.company.MyApp.Keyboard"
let installedKeyboards = NSUserDefaults.standardUserDefaults().objectForKey("AppleKeyboards")
println(installedKeyboards)
}
这是它在控制台中返回的内容:
Optional((
"en_GB@hw=British;sw=QWERTY",
"emoji@sw=Emoji",
"com.nuance.swype.app.Global-Keyboard",
))
我很难检查我的bundleID
是否在此返回的对象中。我尝试了for in
和if(contains(x,x))
,但无法构建。任何帮助将非常感激。
答案 0 :(得分:3)
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")
}
}
}
答案 1 :(得分:1)
您在那里得到Optional
回复,这意味着该值可能为nil
。试着这样做:
if let installedKeyboards = NSUserDefaults.standardUserDefaults().objectForKey("AppleKeyboards") {
if (contains(installedKeyboards, "Your keyboard") {
// Do stuff.
}
}
答案 2 :(得分:0)
这是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")
}
}
}