Lockbox icon indicating copy to clipboard operation
Lockbox copied to clipboard

Occasional Nil value for String.

Open ghost opened this issue 10 years ago • 3 comments

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?

ghost avatar Aug 20 '15 17:08 ghost

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?

granoff avatar Aug 20 '15 21:08 granoff

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

ghost avatar Aug 21 '15 06:08 ghost

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.

granoff avatar Aug 21 '15 14:08 granoff