Data loss due to [NSUserDefaults standardUserDefaults] not being accessible when the device is not unlocked after a reboot.

Originator:zkiraly
Number:rdar://16761393 Date Originated:4/29/2014
Status:Open Resolved:
Product:iOS SDK Product Version:7.1
Classification:Data loss Reproducible:Yes
 
An app can become active in the background due to a Significant Location Change (SLC). To properly initialize itself, the app may need to load values from [NSUserDefaults standardUserDefaults]. When the user reboots the iPhone due to battery exhaustion, OS update, or explicitly, the app can be woken up in the background due to a SLC, before the user had a chance to unlock the iPhone. [NSUserDefaults standardUserDefaults] are always protected until first unlock. When the app reads values from [NSUserDefaults standardUserDefaults], nil values are returned, because the phone has not been unlocked since reboot. It has been observed, that after the subsequent "first unlock", the contents of [NSUserDefaults standardUserDefaults] are blank. This has been observed on iPhones protected by a passcode.

And if [[[NSUserDefaults standardUserDefaults] synchronize] is called while the app is active before the first unlock after a reboot, the data stored in [NSUserDefaults standardUserDefaults] will be lost.

Comments

any update?

did you receive any response to this bug? we've definitely seen it all the way from ios 7 to ios 9. http://www.openradar.me/17813085 suggests it was fixed in ios 8, but perhaps they only fixed clobbering them on disk? our latest problem on ios 9 was that after a background launch before unlock standardUserDefaults remained cached in memory as empty even though we called resetStandardUserDefaults after the unlock. upon killing and re-launching the app they were all back just fine.

https://devforums.apple.com/message/1122437#1122437 http://stackoverflow.com/questions/23443950/nsuserdefaults-values-are-lost-periodically http://stackoverflow.com/questions/20269116/nsuserdefaults-loosing-its-keys-values-when-phone-is-rebooted-but-not-unlocked http://stackoverflow.com/questions/20464964/why-are-nsuserdefaults-not-read-after-flat-battery-ios7


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!