Tuesday, August 31, 2010

HOW TO: Read/write files in iOS

One of the biggest questions for a new (and sometimes current) iOS developer is "how do I save user's input in iOS4?"

There are three ways to do this, but I will only cover one of them (the others can be figured out with a few modifications to the below code).

We will read and write some data into a plist file.  Think of a plist like a TableView, with each row of information on a separate line.

First, you must tell iOS4 where the Document folder is in the OS.  To do this, type in:


-(NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"yourfile.plist"];
}

The above code says: "The data file will be written in a NSArray and the paths to the file will be in the documentsDirectory.  The stringByAppendingPathCompoent is the filename to the data file, in this case "yourfile.plist".

Now that we've told iOS4 where to write the file, we need to tell it WHEN to write it.  Their are three ways to do this:

1.  When a user exits the app.
2.  When a user presses a SAVE button.
3.  IOS4 - When a user goes out of the app into a new one (go into the background).

The one common mistake that a lot of developers make (including me) is they forget to tell iOS4 to "pay attention" to when the app goes into the background, or when the app terminates when a user presses the HOME button.  Somewhere in the code (I put mine at the beginning) you must "force" the OS to keep those commands in RAM.  To do so, use:

 UIApplication *command1 = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:command1];

Lets break the command down:

1.  UIApplication - You want to interact with your app. to set up a pointer.
2.  *command1 - Variable to be used to identify which command is which.
3.  NSNotificationCenter - You want to "notify" the OS that you're using a certain command.
4.  applicationWillTerminate - The name of the notification that you want to use.
5.  UIApplicationWillTerminate - It's related identifier for the OS.
6.  addObserver - Look at 'itself' (the entire command) for instructions.
7.  @sector - Which notification do you want to use.
8.  name: The UI name of the notification.
9.  object: What you defined in the beginning (in this case, *command1).

Now that we told iOS which command to look for, we call it where ever we want to save the file - in this case, when the user presses the HOME button.

// Save Data on Application Termination:
-(void)applicationWillTerminate:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:text.text];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}

Note -

* The text.text is a name of the textView (or textField) that you wish to save.
* The writeToFile line would be the datafilepath to the Document's directory that you defined earlier.
* The atomically: just says to write the file AUTOMATICALLY.
* Release the array before the app terminates.

Now, to load the data when the app loads, just do the following:

-(void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
text.text = [array objectAtIndex:0];
[array release];
}

You'll just reverse what you did to save the file but you're LOADING the contents.

1.  When the viewController loads, load the file.
2.  If there is NOT a file to load, create one.
3.  If there IS a file, load the file into an Array.
4.  text.text is the variable that you want to load into the viewController.
5.  Release the array.

That's all for now.  Enjoy!

No comments:

Post a Comment