We are currently experiencing the following weird issue with our iPhone app. As the title says, NSUserDefaults
is losing our custom keys and values when phone i
We also experienced this problem when using significant location change, on devices with pass code enabled. The app launches on BG before the user even unlock the pass code, and UserDefaults has nothing.
I think it is better to terminate the app before the synchronize occurs, because the reasons below:
So here's our (a bit weird) workaround. The app kills itself immediately when the situation (app state = BG, UserDefaults is cleared, iOS >= 7) detected.
It should not violate UX standard, because terminating app on background will not be even noticed by the user. (And also it occurs before the user even passes the pass code validation)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
+ (void)crashIfUserDefaultsIsInBadState
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
&& [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"firstBootDate"]) {
NSLog(@"------- UserDefaults is healthy now.");
} else {
NSLog(@"----< WARNING >--- this app will terminate itself now, because UserDefaults is in bad state and not recoverable.");
exit(0);
}
}
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"firstBootDate"];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.class crashIfUserDefaultsIsInBadState]; // need to put this on the FIRST LINE of didFinishLaunchingWithOptions
....
}