Pembaruan : Masalah ini telah diperbaiki di Xcode 8.2. Rantai kunci berfungsi di simulator tanpa mengaktifkan berbagi rantai kunci.
Mengapa saya selalu menerima kesalahan -34018 saat memanggil SecItemAdd
fungsi di simulator Xcode 8 / iOS 10 ?
Langkah-langkah untuk Mereproduksi
Buat halaman Tunggal iOS proyek aplikasi baru di Xcode 8. Jalankan kode berikut di viewDidLoad
(atau membuka ini proyek 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.")
}
hasil yang diharapkan
Item ditambahkan ke Keychain.
Hasil nyata
Fungsi SecItemAdd mengembalikan kode kesalahan berikut: -34018
.
Versi: kapan
Xcode versi 8.1 (8B62), macOS Sierra 10.12.1.
Konfigurasi
Selalu terjadi di Xcode 8 sejak Beta 2 saat menguji di simulator iOS 10.
TIDAK terjadi di Xcode 8 saat menguji di simulator iOS 9.3.
Demo
https://dl.dropboxusercontent.com/u/11143285/2016/07/KeychainBugDemo.zip
Referensi
Radar: https://openradar.appspot.com/27422249
Forum Pengembang Apple: https://forums.developer.apple.com/message/179846
Masalah ini berbeda dari posting berikut ini karena terjadi secara konsisten di Xcode 8. SecItemAdd dan SecItemCopyMatching mengembalikan kode kesalahan -34018 (errSecMissingEntitlement)