evgenyneu/keychain-swift

Keychain retain a nil after long time in background

GuyKahlon opened this issue · 14 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

Thanks :)

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

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

mzaks commented

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.

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, that's good to know. We can discuss your background access problems in #78.

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?

mzaks commented

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.

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

Possible unconfirmed workaround is here.

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 - #78 (comment)

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