granoff / Lockbox

Objective-C utility class for storing data securely in the key chain.
MIT License
849 stars 87 forks source link

Occasional Nil value for String. #42

Open ghost opened 9 years ago

ghost commented 9 years ago

At the moment I am using Lockbox to store a sessionToken in keychain.

Occasionally Lockbox returns a nil value when returning the sessionToken string.

Should i be returning the value from Lockbox every time I make a network request, or should I be assigning it to a property within the application when it's first retrieved?

Is there an issue with getting data stored in keychain?

granoff commented 9 years ago

How are you assigning the value returned from objectForKey:? If you are assigning it to a strong instance variable using dot notation, I would expect you to have access to the value all the time. If you are retrieving the value and assigning it to a variable that is something other than strong, I am not surprised that it becomes nil eventually, as the OS is releasing it because it thinks you're done with it. Can you post some code where you retrieve the value, etc?

ghost commented 9 years ago

I've only just started assigning the value to a strong property, but the main issue stems from the following post on StackOverflow.

http://stackoverflow.com/questions/10536859/ios-keychain-not-retrieving-values-from-background/10583042#10583042

granoff commented 9 years ago

Ah! I see. That post talks about the problem being solved by specifying a more permissive accessibility for the data. Lockbox supports data accessibility, the default being kSecAttrAccessibleWhenUnlocked.

All the setXxx:forKey: methods use this default value. But each set method has a version available that lets you specify the accessibility you prefer, e.g. setString:forKey:accessibility:. So for the data you need to access from the key chain while the device is locked or the app is backgrounded, use one of these methods with a more permissive accessibility, e.g. kSecAttrAccessibleAfterFirstUnlock.