Wednesday, July 28, 2010

How to Save the Contents of an Array to the iPhone’s Filesystem


The simplest way to save user preference/data on iPhone is using NSUserDefaults. Here comes another easy way to save data in filesystem.

In this example, we are going to create an array, fill it with values and then save the contents of the array to the documents directory of our app.

Then, after stopping the program and putting in code, we will retrieve the array from the filesystem. Finally, to prove that it all worked we will use an alert box to inform the user of what is in the array that we stored.

First, in the applicationDidFinishLaunching method put in this code:
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/newFileArray", documentsDirectory];

//create an array and add values to it:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:@"First string"];
[array addObject:@"Second string"];
[array addObject:@"Third string"];

//this statement is what actually writes out the array
//to the file system:
[array writeToFile:fullFileName atomically:NO];

Now, your information has been saved to the iPhone’s file system in the documents directory of your app. Here is how you would retrieve the information that you saved:
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/newFileArray", documentsDirectory];

//retrieve your array by using initWithContentsOfFile while passing
//the name of the file where you saved the array contents.
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
NSLog(@"%@", array);

That's it – a quick and easy way to save information on the iPhone. The array is easily saved and retrieved from iPhone filesystem.

Tuesday, July 13, 2010

UIAlertView with a TextField within it

Every iphone developer would have seen default alert view with a title, a message and buttons to dismiss the alert view. Today I will show an option to insert an UITextField inside a UIAlerView.

We will basically call a UIAlertView and then add the UITextField programmatically.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Enter Your Name” message:@”message hidden by text field” delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles:@”OK!”, nil];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];


We are including a dummy message in the alert view because it's position will be occupied by our textfield. If we didn’t put that sentence then the alerts buttons would go up more and the UITextField will be messed up.

Next thing that we have used is CGAffineTransform. It is used because on focusing on the textfield, keyboard appears and covers some part of our UITextField. Hence this transform ensures that keyboard doesn't covers it up.

Now build and Run the application. You will get a screen as below.