views:

66

answers:

1

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!

A: 

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];
}
taskinoor
I'm trying to save the data - the objects and the order of them
Guy Dor
got it. editing my answer with the sample code.
taskinoor
In this line: NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];What kind of file should I create?Thank you very much!
Guy Dor
An array will be saved as plist. So you should use .plist as extension.
taskinoor
As extension for what?Should I create a .plist file?
Guy Dor
FILE_NAME = @"myArray.plist". You don't need to create a file. writeToFile method will create one for you. You only need to pass the required full file name.
taskinoor
Thank you, I did it but it still don't save my objects
Guy Dor
Then there might be some mistake. Where are you looking for the file? It's better if you post some code.
taskinoor
Here's the delegate.m file code:- (void)applicationWillTerminate:(UIApplication *)application { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; //NSString *savigFile; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"alertsArray.plist"]; // save the file [[rootViewController alertsArray] writeToFile:filePath atomically:TRUE]; }The NSMutableArray is declare at the RootViewController file
Guy Dor
Code seems fine. Where are looking for the saved file? For simulator it will be home_directory/Library/Application Support/iPhone Simulator/User/Application/an_application_id
taskinoor
So what do you say is that I should create a .plist file named alertsArray.plist and then do what?Thank you for your patience!
Guy Dor
I have already said that you don't need to create the file. NSArray method writeToFile will create the file. After terminating the app, check the above directory that whether the file is created or not. If not, then there is some other problem. Also try to debug the app, say whether the applicationWillTerminate is calling, what is the return value for the file path etc.
taskinoor
Nope, it didn't create the file
Guy Dor
debug the app and check what is the value of filePath and savingFile before writeToFile is called during termination. and i dont understand what u r trying to do with the BOOL method. and also where r u looking for the saved file? i mean in which directory?
taskinoor
The folder is empty
Guy Dor
Have you tried to debug the app? What is the filePath? It seems that rather than making these comments, it's better to provide you a sample project.
taskinoor
I tried to debug the project but the folder is still empty
Guy Dor
What do u by mean of debug? I am saying what is the value of file path? Have you checked that? I'm sure you are either making some mistake or looking in the wrong directory. If you can't post the code or debug state, it's not possible to help further.
taskinoor
Here's my "applicationWillTerminate" code:Delegate.m- (void)applicationWillTerminate:(UIApplication *)application { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *savingFile; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:savingFile]; [[rootViewController alertsArray] writeToFile:filePath atomically:YES]; NSLog(@"filePath: %@", filePath); NSLog(@"savingFile: %@", savingFile); }
Guy Dor
What is the output of NSLog? It seems that you are passing nil value to filePath = [documentsDirectory stringByAppendingPathComponent:savingFile]; as you have not set any value to savingFile.
taskinoor
Output of NSLog for what?
Guy Dor
NSLog that u used in ur code
taskinoor