Home > Enterprise >  Error when saving to keychain using SecItemAdd
Error when saving to keychain using SecItemAdd

Time:08-17

I'm getting an error saving an encoded value to keychain at the point of SecItemAdd. I'm fairly new to working with Keychain and not sure how to return the error to see what I'm doing incorrectly.

let encoder = JSONEncoder()


  func initiateLogin(forceReconnect: Bool = false, completion: @escaping (Bool)->Void) {
      Task {
          await loginUser(forceReconnect: forceReconnect, completion: { user in

              if let encoded = try? self.encoder.encode(user) {

                  // MARK: - keychain

                  let attributes: [String: Any] = [
                    kSecClass as String: kSecClassGenericPassword,
                    kSecAttrAccount as String: "johnDoe",
                    kSecValueData as String: encoded,
                  ]

                  if SecItemAdd(attributes as CFDictionary, nil) == noErr {
                      print("\(#function)            
  • Related