SwiftKeychainWrapper icon indicating copy to clipboard operation
SwiftKeychainWrapper copied to clipboard

Data in Keychain missing

Open ablkenny opened this issue 7 years ago • 18 comments
trafficstars

hi guys, i am having issues using KeychainWrapper in Swift 3.1.

everything was fine in the beginning. but after a period of time, data stored in keychain is lost and unable to retrieve.

I had stored the data with the default accessibility (.whenUnlocked), and attempt to retrieve using the same accessibility while the app is in foreground.

The issue is quite random as not all iphone devices encounter it.

Does anyone encountered this issue before? Any solution or is keychain stable to use?

ablkenny avatar Jan 19 '18 04:01 ablkenny

Seeing the same thing on Swift 4

Phoenix7351 avatar Jan 25 '18 22:01 Phoenix7351

Same issue on Swift 3.2.

@ablkenny Did you find a pattern?

nicopuri avatar Feb 14 '18 00:02 nicopuri

hey @ablkenny @nicopuri I saw this issue recently and realized that the keychain is specific to the applications 'identifier entitlement'.

In simple terms, the build I run on test flight with a production provisioning profile does not have access to the same keychain data as if I do a local dev build with a dev provisioning profile. However, there may be more cases that could trigger this same issue, I'm not sure.

Perhaps this is related to this problem you are experiencing

logansease avatar Feb 28 '18 15:02 logansease

I too experience the same issue, any take on how to fix it?

Dbigshooter avatar May 04 '18 11:05 Dbigshooter

Try this https://github.com/kishikawakatsumi/KeychainAccess

zapjonny avatar May 16 '18 05:05 zapjonny

I did not managed to resolve this issue. What I did was to migrate keychain data into core data with additional encryption.

Not sure if this is the right approach. Will try to do more investigation and update here again

ablkenny avatar Jun 06 '18 02:06 ablkenny

Same. The data of encoded Array is not storred. Trying the lib, @zapjonny mentioned. ... Tried. This lib is better for use. Made it work. Take sure, that u use

KeychainWrapper.standard.set(encoded, forKey: "") 
let archivedData = KeychainWrapper.standard.data(forKey: "") 

NikKovIos avatar Jul 20 '18 09:07 NikKovIos

Hi, I use this lib and I can find error like you. I found the pattern like this.

1. app terminated and start
2. app on background for a long time (on background after 3~4 hours)
3. iOS 11+

I saw my code again and again, but I can't find error logic.. :(

My environment

  • Xcode 9.2
  • iOS 11+
  • Swift 3.2
  • KeyChainWrapper 3.0.1

devssun avatar Jul 30 '18 07:07 devssun

I recommend checking out the Valet api by Square. https://github.com/square/Valet

logansease avatar Jul 30 '18 14:07 logansease

@logansease Valet api ? what is that?

devssun avatar Jul 31 '18 08:07 devssun

@hyesunzzang https://github.com/square/Valet

logansease avatar Jul 31 '18 13:07 logansease

I change the lib https://github.com/kishikawakatsumi/KeychainAccess but it happens again.. 😢

devssun avatar Aug 01 '18 08:08 devssun

This is still a problem, it keeps making my user to logged out when I tried to store their data in keychain, I thought it was the accessibility problem, so I changed it to .afterFirstUnlock, still missing. Gonna change to Valet to see if it works.

The build has problem is the Testflight build, I'm running iOS 13 in iPhone XS, but it might happen with any other device

Tj3n avatar Oct 17 '19 02:10 Tj3n

You may be encountering a situation where protected data is not yet available. Under some rare circumstances your app may be launched (perhaps from a push notification) and UIApplication.shared.isProtectedDataAvailable returns false. If this happens you will not be able to access values stored in the Keychain (or UserDefaults)

eliburke avatar Oct 17 '19 14:10 eliburke

Thanks, sound like exactly what Im facing, my app indeed have a lot background wake, is there anyway to overcome this?

tj3n123 avatar Oct 17 '19 16:10 tj3n123

Yes. Always check UIApplication.shared.isProtectedDataAvailable in your appDelegate's application:didFinishLaunchingWithOptions before trying to access Keychain or UserDefaults. If protected data is not available and you require it, you could try to delay / defer startup in some way (such as Timer or asyncAfter)

Some more reading: https://stackoverflow.com/questions/43125168/ios-app-startup-and-protected-data-events https://stackoverflow.com/questions/48002584/how-to-process-tap-on-push-notification-banner-at-password-locked-ios-device https://medium.com/@bohan_66764/dont-let-nsuserdefaults-betrayed-you-ccaa41013592

eliburke avatar Oct 17 '19 16:10 eliburke

I try solve this problem, but I can't so I SwiftKeychainWrapper replace to SQLite.

https://github.com/stephencelis/SQLite.swift

devssun avatar Oct 20 '19 12:10 devssun

For some reason, Vallet lib seems to completely solved this issue for me. If anyone have this problem try switch to it.

tj3n123 avatar Oct 21 '19 00:10 tj3n123