0

This function writes to the keychain properly when testing on both the simulator and the device. It does not work when installed with an enterprise Signing Identity.

It is worth mentioning Keychain Sharing is enabled in the app and the app's extension.

Please note it works perfectly on the device when building with the development profile.

Additionally no errors are logged in the operation (see below) so there isn't any information as to why it failed.

public class func setToken(token: String, account: String, service: String = "APIrev1") {

    var log = XCGLogger.defaultInstance()

    log.info("Attempting to set token \(token) to account \(account)")

    var secret: NSData = token.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
    let objects: Array = [secClassGenericPassword(), service, account, secret]
    let keys: Array = [secClass(), secAttrService(), secAttrAccount(), secValueData()]
    let query = NSDictionary(objects: objects, forKeys: keys)
    var p1 = SecItemDelete(query as CFDictionaryRef)
    if (p1 != noErr) {
        log.info("SecItemDelete, key not present to delete")
    }
    var p2 = SecItemAdd(query as CFDictionaryRef, nil)
    if (p2 != noErr) {
        log.error("SecItemAdd write of token \(token) to account \(account) failed")
    }
}
Nick
  • 1,194
  • 1
  • 10
  • 18
  • There might be a lower-level issues with keychain in iOS8.1. See http://openradar.appspot.com/18776721 – Code Roadie Jan 16 '15 at 08:15
  • @CodeRoadie I would welcome even an error. SecItemDelete SecItemAdd don't do anything and fail silently. – Nick Jan 16 '15 at 19:13
  • Also: var p1 = SecItemDelete(query as CFDictionaryRef) The value of SecItemDelete is 0. var p2 = SecItemAdd(query as CFDictionaryRef, nil) The value of SecItemAdd is 0. 0 is not an error. I don't know what it is but it is not an error. – Nick Jan 16 '15 at 19:27
  • I believe this is a bug in Swift iOS that only occurs in 64 bit devices. This solution worked but I have filed a report: http://stackoverflow.com/a/27721235/737470 – Nick Jan 18 '15 at 07:24

1 Answers1

0

I believe this is a bug in Swift iOS that only occurs in 64 bit devices. This solution worked but I have filed a report: https://stackoverflow.com/a/27721235/737470

Community
  • 1
  • 1
Nick
  • 1,194
  • 1
  • 10
  • 18