Thursday, October 7, 2010

Hello again

I've been waiting to post more articles until I could get the word out about this blog.  Stay tuned, more articles are on the way!

Friday, September 3, 2010

XCode 4 Preview 3 Now Available

iOS4 official logo
Apple has seeded another preview version of XCode 4 to all registered developers.  It's available now at http://developer.apple.com/iphone.

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!

Friday, August 27, 2010

Tip: How to install two Versions of XCode

As a developer, I am always looking towards the future of what Apple is making available for us to make development time easier and faster.  When they put out a beta version of XCode, a lot of new developers assume they can overwrite the current version with a Beta, and still submit apps to the app store.  This, unfortunately, is not the case.

When using a Beta version of an application, keep 3 things in mind:

- The app may have hidden errors that may crop up at any time.
- The Frameworks may not work as expected.
- Any Obj-C commands may not work on an actual device.

Betas go through three stages:

1.  Beta stage - for internal testing only.  NOT for public use.
2.  GM - App is 99.9% ready for public release.  Minor bugs may still be present.
3.  Final - Final version.

Always install a beta version of XCode in a separate folder, such as XCode Beta or something close to that. Then, when Apple comes out with a final version, install it in the normal XCode folder, and remove the beta version from your computer (removal instructions are always included with any version of XCode, in the XCode documentation).

If you want to test a current application in a Beta, copy it to another folder, leaving the original in tact in case you have to make changes to a working copy and make it available on the app store quickly.

Wednesday, August 25, 2010

HELLO AND WELCOME!


Welcome to "iOS Help" web site. This site is dedicated to helping iOS developers get the help and information they are seeking when developing for the iOS platform.

Let's get started!