0

We are storing some user info in the Keychain. We have received reports that for some users that have data stored in the keychain the retrieval is failing.

Under what circumstances would a kSecClassGenericPassword attribute would fail?

Code:

let query = [
            kSecClass as String       : kSecClassGenericPassword,
            kSecAttrAccount as String : key,
            kSecReturnData as String  : kCFBooleanTrue!,
            kSecMatchLimit as String  : kSecMatchLimitOne ] as [String : Any]

        var dataTypeRef: AnyObject? = nil

        let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)

Code for store:

func save(key: String, data: Data) -> OSStatus {
        let query = [
            kSecClass as String       : kSecClassGenericPassword as String,
            kSecAttrAccount as String : key,
            kSecValueData as String   : data ] as [String : Any]

        SecItemDelete(query as CFDictionary)

        return SecItemAdd(query as CFDictionary, nil)
    }
    

Mention : The code for retrieving the data can be called multiple time a second . Could this "stress" the keychain?

user426132
  • 1,341
  • 4
  • 13
  • 28
  • Does this answer your question? [Intermittent & Temporary iOS Keychain Failure](https://stackoverflow.com/questions/51830841/intermittent-temporary-ios-keychain-failure) – Derek Lee Jul 06 '23 at 01:06

1 Answers1

1

Depending on what access rights you specified when the item was created, it may be impossible to retrieve it for example if the user has no password, had their phone locked, turned off or changed biometrics, or restored the app on another phone from a backup.

gnasher729
  • 51,477
  • 5
  • 75
  • 98