Keychain fails on iOS 10 simulator

Originator:douglashill
Number:rdar://27844971 Date Originated:15-Aug-2016 01:22 PM
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:Always
 
Summary:
Accessing the Keychain from the iOS 10 simulator using Xcode 8 beta 2–5 fails with error code -34018 unless Keychain Sharing is enabled.

Steps to Reproduce:
1. Create a new Single page iOS app project in Xcode 8 beta 3.
2. Run the following code in viewDidLoad (or open the attached Xcode project).

    let itemKey = "My key"
    let itemValue = "My secretive bee 🐝"
    
    // Remove from Keychain
    // ----------------
    
    let queryDelete: [String: AnyObject] = [
      kSecClass as String: kSecClassGenericPassword,
      kSecAttrAccount as String: itemKey
    ]
    
    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,
      kSecValueData as String: valueData,
      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.")
    }

Expected Results:
Item is added to Keychain

Actual Results:
Function SecItemAdd returns the following error code: -34018

Version:
Xcode version 8.0 beta 5 (8S193k)
OS X version 10.11.5 (15F34)

Notes:
Workaround: enable Keychain Sharing in the target settings’ Capabilities tab. Then Keychain works as expected.

This is a duplicate of rdar://27422249 although I added more detail here.

Discussion: http://stackoverflow.com/questions/38456471/secitemadd-always-returns-error-34018-in-xcode-8-beta-4-in-ios-10-simulator

Configuration:
Always occurs in Xcode 8 beta 2–5 when testing in iOS 10 simulator (14A5309d).

Does NOT occur in Xcode 8 beta 2 and beta 3 when testing in iOS 9.3 simulator.

Does NOT occur in Xcode 8 beta 1 when testing in iOS 10 simulator (14A5261u).

Attachments:
'KeychainBugDemo.zip' was successfully uploaded.

Comments

Any Entitlement Works

I found any Entitlement works, it's not specific to the Keychain Sharing entitlement.

By patrick.mccarron at Sept. 20, 2016, 8:52 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!