Hi Readers,
In any iPhone app if it is needed to iterate or remove NSUserDefault keys (as in logout feature), following code can be used:
To iterate key objects:
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
//Here you can log the key
NSLog(@"Key object= %@",[dict objectForKey:key]);
}
[defs synchronize];
To remove objects from keys:
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
//Here you can log the key
[defs removeObjectForKey:key];
}
[defs synchronize];
Or for remove data from NSUserDefauls one can use simply following code as well:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
HAPPY CODING :)
0 Comment(s)