Keychain Query Always Returns errSecItemNotFound After Upgrading to iOS 13

前端 未结 5 1259
春和景丽
春和景丽 2021-01-31 09:01

I am storing passwords into the iOS keychain and later retrieving them to implement a \"remember me\" (auto-login) feature on my app.

I implemented my own wrapper aroun

5条回答
  •  再見小時候
    2021-01-31 09:46

    Regarding the issue in kSecClassGenericPassword, I was trying to understand what is the problem and I found a solution for that.

    Basically it seems like Apple was fixing an issue with kSecAttrAccessControl, so below iOS version 13 you add keyChain object with kSecAttrAccessControl without biometric identity and above iOS 13 that does not work anymore in a simulator.

    So the solution for that is when you want to encrypt the keyChain object with biometric you need to add kSecAttrAccessControl to your query but if you don't need to encrypted by biometric you need to add only kSecAttrAccessible that's the right way to do these.

    Examples

    Query for biometric encrypt:

    guard let accessControl = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
                                                              kSecAttrAccessibleWhenUnlocked,
                                                              userPresence,
                                                              nil) else {
                                                                  // failed to create accessControl
                                                                  return 
                                                              }
    
    
    var attributes: [CFString: Any] = [kSecClass: kSecClassGenericPassword,
                                               kSecAttrService: "Your service",
                                               kSecAttrAccount: "Your account",
                                               kSecValueData: "data",
                                               kSecAttrAccessControl: accessControl]
    

    Query for regular KeyChain (without biometric):

    var attributes: [CFString: Any] = [kSecClass: kSecClassGenericPassword,
                                                   kSecAttrService: "Your service",
                                                   kSecAttrAccount: "Your account",
                                                   kSecValueData: "data",
                                                   kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly]
    

提交回复
热议问题