I need to save a NSMutableDictionary in NSUserDefaults. I have searched the web for many examples, but have not received any ones that are relevant enough. Can somebody post a small piece of code which will save a NSMutableDictionary in NSUserDefaults at shutdown and retrieve it at launch of the app? Also could you please tell me where to place the code.
I have some code myself, but it is not working.
appDelegate.h - retrieval of data
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *dat = [defaults objectForKey:@"theKey"];
tmpArray = [[NSMutableArray alloc] init];
tmpArray = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
[self.window makeKeyAndVisible];
[window addSubview:tabBarController.view];
return YES;
}
appDelegate.h - storage of Data
- (void)applicationWillTerminate:(UIApplication *)application {
[self saveContext];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *dat = [NSKeyedArchiver archivedDataWithRootObject:tmpArray];
[defaults setObject:dat forKey:@"theKey"];
}
Yes, it is for an array, but I will need one for a NSMutableDictionary.