Skip to content

Instantly share code, notes, and snippets.

@quicklywilliam
Created October 1, 2013 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quicklywilliam/6774038 to your computer and use it in GitHub Desktop.
Save quicklywilliam/6774038 to your computer and use it in GitHub Desktop.
Work around for rdar://problem/15115056 Bug description: If an iOS device has been rebooted and but not yet unlocked, [NSUserDefaults standardUserDefaults] will return nil for any key you've stored. It continues to behave this way even after you unlock the device, until you force quit and relaunch the app.
@interface NSUserDefaults (Fix15115056)
- (void)synchronizeAndSetAsUnprotected;
@end
@implementation NSUserDefaults (Fix15115056)
- (void)synchronizeAndSetAsUnprotected;
{
[self synchronize];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *prefsFilePath = [libraryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Preferences/%@.plist",[[NSBundle mainBundle] bundleIdentifier]]];
if ([fileManager fileExistsAtPath:prefsFilePath]) {
NSMutableDictionary *attr = [[fileManager attributesOfItemAtPath:prefsFilePath error:nil] mutableCopy];
[attr setObject:NSFileProtectionNone forKey:NSFileProtectionKey];
[fileManager setAttributes:attr ofItemAtPath:prefsFilePath error:nil];
}
}
@end
@quicklywilliam
Copy link
Author

In case it is not obvious: this will make contents of NSUserDefaults be stored WITHOUT DATA PROTECTION. The default as of iOS 7 is to store all files with data protection (hence the bug).

You shouldn't store anything that's sensitive in NSUserDefaults anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment