After a while, Apple recognised this as an official bug. So we are only left with different workarounds until it's solved:
If you need the data while executing BEFORE the phone has been unlocked use one of the following options and set the
NSPersistentStoreFileProtectionKey = NSFileProtectionNone
option:- Save data using Core Data. (If you need to access the DB in background when the phone wasn't unlocked yet AND you don't have sensible information in it, you can add to the options Array the following option:
NSPersistentStoreFileProtectionKey = NSFileProtectionNone
) - Use the Keychain.
- Use a .plist file.
- Use custom made files: (e.g.: .txt with a specific format).
- Any other way you might find comfortable for storing data.
Choose yours ;)
- Save data using Core Data. (If you need to access the DB in background when the phone wasn't unlocked yet AND you don't have sensible information in it, you can add to the options Array the following option:
If you don't need or don't care about the data before the phone has been unlocked you can use this approach (Thanks @maxf):
Register to the
applicationProtectedDataDidBecomeAvailable:
notification and execute the following line of code inside the callback[NSUserDefaults resetStandardUserDefaults]
This will make you
NSUserDefault
reload right after your phone has been granted permission to access protected data, helping you to avoid this issue entirely.
Thanks all for the help!