keychain-swift icon indicating copy to clipboard operation
keychain-swift copied to clipboard

Keychain retain a nil after long time in background

Open GuyKahlon opened this issue 9 years ago • 15 comments

Hi,

I have a serious inconsistent problem when the app long time in the background.

In my app I save the user finger print in the keychain, when the app launch (at didFinishLaunchingWithOptions funk) I check if the is a finger print saved in the keychain, if there isn't finger print saved in the keychain I replace my Main story board with the Register storyboard.

Note: I also save a flag in the NSUserDefault to check if this is a first launch in order to reset (clear all the data) the keychain if it's a first launch.

The problem is, after a long time in the background (sometimes not all the time) when I open the app the Register Storyboard appear. Means the app terminates by the OS in the background and there isn't finger print in the keychain. In the beginning I thought the finger print was removed from the keychain, but, if I terminate mentally the app and open it again, everything is good and the main story board appears as expected.

Note: The fingerprint is a String and I saved it in the keychain without any Access key.

Any idea?

Thanks

GuyKahlon avatar Jan 10 '16 09:01 GuyKahlon

Hi, this is a very serious Keychain problem, thanks for raising this up. I noticed it myself many times in the past. Keychain sometimes returns an error when I try to retrieve a value from it. There are many people struggling with issues that may be related to your problem. I do not have a solution, unfortunately.

https://forums.developer.apple.com/thread/4743

https://github.com/kishikawakatsumi/UICKeyChainStore/issues/20

https://github.com/soffes/sskeychain/issues/75

evgenyneu avatar Jan 10 '16 20:01 evgenyneu

Thanks :)

GuyKahlon avatar Jan 11 '16 07:01 GuyKahlon

@GuyKahlon I am facing the exactly same thing with UICKeyChainStore library.

rashidasgari avatar Feb 22 '16 08:02 rashidasgari

Anybody find a fix for this? I am having identical issue.

jlgaustin avatar May 24 '17 15:05 jlgaustin

Hi there,

AFAIK the problem is it takes time to decrypt the key chain. If you have your AccessOption set to accessibleWhenUnlocked and try to read in app delegate didFinishLaunchingWithOptions than it could happen that you get nil as the data is not decrypted. Solution would be to either switch to less restrictive AccessOption, or to access the values in the keychain a bit later. Giving the key chain time to decrypt. This is what helped me at least.

mzaks avatar May 30 '17 12:05 mzaks

I can confirm this occurs no matter what the access level is. We have 1.5-2mil background wakeup events a day. Of those, ~5% of users who do have a user fingerprint stored in their keychain will get a nil value.

RamblinWreck77 avatar Apr 09 '18 21:04 RamblinWreck77

@RamblinWreck77, that's good to know. We can discuss your background access problems in #78.

evgenyneu avatar Apr 09 '18 22:04 evgenyneu

Hi, I'm facing this problem, after many hours of inactivity, when I open the application, my manager fails to retrieve the refreshToekn from the keychain and the user is redirected at the login page.

Any tip?

AndreaMiotto avatar Nov 21 '18 09:11 AndreaMiotto

Hi, I'm facing this problem, after many hours of inactivity, when I open the application, my manager fails to retrieve the refreshToekn from the keychain and the user is redirected at the login page.

Any tip?

Is your manager called from AppDelegate? You need to delay the call some how, AFAIK it's all about timing, the keychain needs time to be decrypted, if it is not yet decrypted it will return nil. So my tip is: either delay the whole thing, or implement a retry, if values are nil for a couple of times.

mzaks avatar Nov 21 '18 09:11 mzaks

Nop, not always is called from the appDelegate. And this is very hard to debug so I think I will move to realm encrypted

AndreaMiotto avatar Nov 21 '18 10:11 AndreaMiotto

Possible unconfirmed workaround is here.

evgenyneu avatar May 26 '19 01:05 evgenyneu

I am also having issues now. It's really intermittent and I can't reproduce it. Some users have the issues, some don't. What I see is that somehow I think it's related to the app being in the background and then an update (to keychain) occurs.

I am gonna add extra logging to see if I can manage to capture where it's happening. I've also streamlined the saving of the keychain so that I don't have multiple calls to it in succession.

with regard to the possible solution - https://github.com/evgenyneu/keychain-swift/issues/78#issuecomment-495959391

This seems to only to do when a notification is received right? My App is basically updating some in-game-currency to the keychain every 60s and doesn't receive any notifications

app4g avatar Dec 09 '23 01:12 app4g

@app4g Facing the same issue. Do you have any findings to share from the additional logging?

sincychacko-frollo avatar Mar 26 '24 04:03 sincychacko-frollo

@sincychacko-frollo What I essentially did was to make sure only 1 instance of command to update the keychain is done at any one time and some caching of the data into memory.

After multiple iterations, I think It's resolved.

sorry.I can't offer any help beyond that.

app4g avatar Mar 26 '24 05:03 app4g