jrendel / SwiftKeychainWrapper

A simple wrapper for the iOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift.
MIT License
1.59k stars 339 forks source link

Data in Keychain missing #105

Open ablkenny opened 6 years ago

ablkenny commented 6 years ago

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?

Phoenix7351 commented 6 years ago

Seeing the same thing on Swift 4

nicopuri commented 6 years ago

Same issue on Swift 3.2.

@ablkenny Did you find a pattern?

logansease commented 6 years ago

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

Dbigshooter commented 6 years ago

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

zapjonny commented 6 years ago

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

ablkenny commented 6 years ago

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

NikKovIos commented 6 years ago

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: "") 
devssun commented 6 years ago

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

logansease commented 6 years ago

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

devssun commented 6 years ago

@logansease Valet api ? what is that?

logansease commented 6 years ago

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

devssun commented 6 years ago

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

Tj3n commented 4 years ago

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

eliburke commented 4 years ago

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)

tj3n123 commented 4 years ago

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

eliburke commented 4 years ago

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

devssun commented 4 years ago

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

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

tj3n123 commented 4 years ago

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