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!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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]; }