【发布时间】:2016-11-22 05:26:50
【问题描述】:
更新:此问题已在 Xcode 8.2 中修复。钥匙串在模拟器中工作,无需启用钥匙串共享。
为什么我在 Xcode 8 / iOS 10 模拟器中调用 SecItemAdd 函数时总是收到错误 -34018?
复制步骤
在 Xcode 8 中创建一个新的单页 iOS 应用项目。
在viewDidLoad 中运行以下代码(或打开this Xcode 项目)。
let itemKey = "My key"
let itemValue = "My secretive bee ????"
// Remove from Keychain
// ----------------
let queryDelete: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject
]
let resultCodeDelete = SecItemDelete(queryDelete as CFDictionary)
if resultCodeDelete != noErr {
print("Error deleting from Keychain: \(resultCodeDelete)")
}
// Add to keychain
// ----------------
guard let valueData = itemValue.data(using: String.Encoding.utf8) else {
print("???????????????????????????????????????? Error saving text to Keychain")
return
}
let queryAdd: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject,
kSecValueData as String: valueData as AnyObject,
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked
]
let resultCode = SecItemAdd(queryAdd as CFDictionary, nil)
if resultCode != noErr {
print("???????????????????????????????????? Error saving to Keychain: \(resultCode).")
} else {
print("???????????????????????????????????? Saved to keychain successfully.")
}
预期结果
项目已添加到钥匙串。
实际结果
函数 SecItemAdd 返回以下错误代码:-34018。
版本
Xcode 版本 8.1 (8B62),macOS Sierra 10.12.1。
配置
在 iOS 10 模拟器中进行测试时,自 Beta 2 起,Xcode 8 中总是出现。
在 iOS 9.3 模拟器中测试时不会出现在 Xcode 8 中。
演示
https://dl.dropboxusercontent.com/u/11143285/2016/07/KeychainBugDemo.zip
参考文献
雷达:https://openradar.appspot.com/27422249
Apple 开发者论坛:https://forums.developer.apple.com/message/179846
此问题与以下帖子不同,因为它在 Xcode 8 中始终发生。SecItemAdd and SecItemCopyMatching returns error code -34018 (errSecMissingEntitlement)
【问题讨论】:
-
这似乎仍然是 Xcode 8 GM 中的一个问题。很高兴看到 Apple 仍处于领先地位...
-
我真的在挖掘你的控制台日志:-)
-
该问题已在 Xcode 8.2 中修复,但在 Xcode 9.0 中又出现了!