Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to iterate or remove all key object from NSUserDefaults

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.84k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: