Hi,
I would like to save objects that the user adds to a NSMutableArray that I will be able to see and use even if I quit the application. I tried to use NSUserDefaults but I'm doing something wrong.
Thanks!
Hi,
I would like to save objects that the user adds to a NSMutableArray that I will be able to see and use even if I quit the application. I tried to use NSUserDefaults but I'm doing something wrong.
Thanks!
What do you mean by "able to see and use even if I quit the application"? Are you trying to use this when the app is not running? Then I'm afraid that will not be possible. Or are you trying to save the data and use that when the app will run again? Then you can write the array to the document directory when the application terminates and load again when the app run again.
In order to save :-
Your app delegate's applicationWillTerminate method is called when terminating the app. You need to save the array in this method.
- (void)applicationWillTerminate:(UIApplication *)application { // get the file path NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; // save the file [myArray writeToFile:filePath atomically:TRUE]; }