Normally while working with iOS applications we create multiple NSUserDefault with multiple names but while a user log out from the application it generally becomes very difficult to remove all the NSUserDefault manually. So I have decided to post the code that I found very useful while implementing my app's logout functionality that helps me to delete all the NSUserDefault allocated to an application at a time.
Code to Remove all NSUserDefault keys:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
NSString *token = [[NSString alloc] init];
token = [NSString stringWithFormat:@"%@",[[NSUsrDefaults standardUserDefaults ] objectForKey:@"device_token"]];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
[[NSUserDefaults standardUserDefaults]synchronize];
In the above code I have deleted all the allocated NSUserDefault to the application. And do remember to synchronize the NSUserDefault for navigating back to home screen.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.