I am trying to logout a user when the application is terminated (since in this particular case the user data doesn’t change very often there is no need to logout when going into background).
So here is my code:
- (void)applicationWillTerminate:(UIApplication *)application
{
User *user = [[User alloc]init];
[user logout];
}
I first tried to log the user out like so but it didn’t work:
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"logged_in"];
}
Any ideas?
If you want the defaults to be updated immediately, you should sync after updating:
As documented (khm, please look up the documentation), the
NSUserDefaultsclass doesn’t immediately persist the changes to disk, only once in a while.