User Defaults unreadable when phone launched from lock screen.

Hi there,

We are noticing a problem with user defaults. We have our data protection policy set NSFileProtectionComplete. When our app is launched in the background because of region monitoring when the phone is locked, user defaults is empty (only contains default system key-value pairs). That makes sense to us because of the policy above. We don't need to read user defaults at this time. The problem lies in the fact that even after we unlock the phone and open the app from the backgrounded state and into the active state, user defaults is still empty. I'm assuming this is because the defaults have been read into memory once and there doesn't seem to be away to force a purge and re-read from disk with our new authorization. Is there any way around this? With this unfortunate behavior, we have 3 solutions in mind:

1) Force crash the app in the background and have it restart when the user opens it from the home screen.
2) Save a backup of user defaults and read that file in when user defaults is empty or just completely get rid of user defaults and write everything to a file / use CoreData.
3) Change our data protection policy to CompleteAfterFirstUserAuthentication.

None of these are ideal changes though. Thanks for your time!
Unfortunately, using data protection policy "NSFileProtectionComplete" has much broader implication than are immediately obvious. Honestly, in hindsight, I think having this as an entitlement was a serious mistake, given it's actual implementation. Case in point, this option:

"3) Change our data protection policy to CompleteAfterFirstUserAuthentication."

Won't really work. The data protection policy is applied at the time files and directories are created and isn't retroactively applied. So changing your data protection policy won't have any effect on your existing files (including NSUserDefaults), unless the file is physically replaced at some point. Further complicating things, the protection level for each file is actually backed up with the file, so even if you "fixed" the problem in a given app version, the problem could reappear at some later point if/when the user restores data from an older backup.

Long story short, my recommendation at this point would be that you designate a particular directory as your "background accessible" directory and then set it and the files inside it to "CompleteAfterFirstUserAuthentication" whenever comes to the foreground. You can do some optimization so you don't need to do EVERY file every time, but I'd be very careful about assuming the protection level is "right".

You can also change you default to "CompleteAfterFirstUserAuthentication" and then iterate your entire app container to "cleanup" the state of your files but, again, you need to be prepared to do that at other points in time, since the problem can reappear unexpectedly.

Hopefully that helps,
Kevin Elliott
DTS, CoreOS/Hardware
User Defaults unreadable when phone launched from lock screen.
 
 
Q