Developing for iPhone

Learn how to create and develop iPhone applications from scratch.

Tutorial 4-2: Keyboard Input

Posted by Henry On July - 26 - 2009 1 COMMENT

Go ahead and open the Story Time project from the previous tutorial. In the resources folder, double-click “FlipsideView.xib” to open the interface for the application’s flipside view. This is where we will have a number of inputs to change the content of the story. By using Labels and Text Fields from the library in Interface Builder, create an interface that looks like this:

iPhone Development Tutorials

iPhone Development Tutorials

Read the rest of this entry »

Tutorial 4-1: Other Templates

Posted by Henry On July - 16 - 2009 1 COMMENT

In the previous tutorial, we used a view-based application template. These consist of just one view or screen. Today, let’s try out a new template. Follow the instructions below to get started:

1. In XCode, click File > New Project
2. In the left pane of the new window, click Application under the iPhone OS heading
3. In the right pane, click Utility Application and then click the Choose button
4. Name the project “Story Time” and click Save.

The Utility Application template we chose this time, sets up a nice flipping function for us. This means we can flip between two different views. Have a look at your new project window. You will notice that in the left pane, we have folder for the main view and a folder for the flipside view. Each of these folders contains some basic classes already set up for us. Now click on the Resources folder. You should see an interface file (.xib) for both the main view and the flipside view. Click “Build and Go” to test it out. Once the simulator is running, you can click the “i” icon at the bottom of the screen to flip over to the other view, then click the “Done” button to flip back. Cool huh?

Read the rest of this entry »

Tutorial 3-3: More Interface Connections

Posted by Henry On May - 2 - 2009 3 COMMENTS

Go ahead and open the “HelloiPhone” project from the previous tutorial. Remember the slider we added to the interface? Today we will give it some functionality.
Open “HelloiPhoneViewController.h” and edit it to look like this:

#import <uikit /UIKit.h>
 
@interface HelloiPhoneViewController : UIViewController {
	IBOutlet UILabel *sliderValue;
}
 
@property (nonatomic, retain) UILabel *sliderValue;
 
-(IBAction) sliderChanged: (id) s;
-(IBAction) goButton;
 
@end
 
</uikit>

Read the rest of this entry »

Tutorial 3-2: Interface Connections

Posted by Henry On May - 1 - 2009 1 COMMENT

Open the “HelloiPhone” project we worked on in the previous tutorial.

Today we will start by adding a button to our interface. So click the Resources folder in the project window and double click “HelloiPhoneViewController.xib” to open the Interface Builder. We already have a label set up, so let’s add a button below it. In Interface Builder, select “Round Rect Button” in the Library window and drag it onto your view just below the label we created. Double click on your new button and type in “GO”. Feel free to move or resize the button however you want. You can resize using the blue dots when the button is selected. When you are happy with your button, proceed to the next step.

We have a button now, but it doesn’t do anything yet. Let’s add a slider and then we can add functionality to them later. Drag a “Silder” from the Library window to your view somewhere below the button. Resize it to fill the width of the view. Now drag another “Label” from the Library and place it below the slider. This label will change when the slider is moved with the help of some code. Change the label to say “0.5″. When the slider is to the left, we will set the slider label to 0 and set it to 1 when it is to the right. Since we are initializing the slider in the middle, the label should initially read 0.5. Highlight the new label and set the alignment to the center. This can be done in the Attributes Inspector. Your interface should now look something like this:

Read the rest of this entry »

How to develop iPhone applications on Windows

Posted by Henry On April - 24 - 2009 ADD COMMENTS

I see a lot of people asking this question.

The simple answer is you can’t. Not at the moment anyway, and there aren’t any signs on the horizon either. You wouldn’t expect Microsoft to allow Mac users to develop XNA games on the Mac would you? Of course, it would be nice. As a primary Windows user myself, I would rather be creating developing iPhone applications in the familiar Windows environment, but it’s just one of those things.

However, don’t be put off by the price of a Mac. The Mac Mini may not be as powerful the iMac or Mac Pro, but its cheap and you dont need much power if you are developing iPhone applications. A Mac Mini has more than enough horse-power for that. On top of that the iPhone SDK (software development kit) is absolutely free!

Tutorial 3-1: Hello iPhone!

Posted by Henry On April - 24 - 2009 1 COMMENT

Ah yes, the moment you have been waiting for. Time to write an iPhone application. If you have not completed the previous tutorials, I suggest you do that now.
Follow these steps to get started:

  1. In XCode, click File > New Project
  2. In the left pane of the new window, click Application under the iPhone OS heading
  3. In the right pane, click View Based Application and then click the Choose button
  4. Name the project “HelloiPhone” and click Save

You will notice that there are a few folders set up for you in the left pane of the project window. For now you don’t need to know what all the files in each folder do.
Click Build and Go. A new application will open called iPhone Simulator, and in it comes a nice shiny iPhone for you to play with. This is where you can test your applications. As soon as it loads, it will boot up the application that you just created. You haven’t done anything to it yet so it is just a gray screen. Press the home button (the square symbol at the bottom) to exit the application. Go ahead and play around with it – use it just like a real iphone but use your mouse to control it instead. Try clicking Hardware > Rotate Left or Right to flip the iPhone on it’s side.

There are some things that the iPhone simulator can’t do. It doesn’t include things like GPS to find it’s location or an accelerometer to detect movement or tilting. You also cant use multi-touch as you only have one mouse. For these things you will need to test on a real iPhone. However, you are only allowed to use a real iPhone if you have paid for and been accepted to Apple’s Standard Developer Program or higher. But for learning the basics, the simulator is fine.

Quit the simulator when you are done and return to XCode.

Read the rest of this entry »

Tutorial 2-4: Inheritance

Posted by Henry On April - 23 - 2009 1 COMMENT

Another reason object-oriented programming languages are so powerful is inheritance. Inheritance lets a class inherit methods and variables from another class. To demonstrate this, let’s make a new class that will inherit the Television class we already created. Open your “Objects Intro” project and click File > New File. Select Cocoa from the left pane and Objective-c Class from the right pane. Click next and name it “MutableTelevision.m” (make sure “create .h” box is ticked) then click Finish.

Ok, now we have two classes in our project. “Television” and “MutableTelevision”. The Television class does the basic things that we would expect a television to do. But what if we want a class that does all that but has extra features? We could write all the methods again for the new class, but an easier option would be to use inheritance and make MutableTelevision a child of Television.

Open “MutableTelevision.h” and let’s start creating the interface file. Edit it to look like this:

#import <cocoa /Cocoa.h>
#import "Television.h"
 
@interface MutableTelevision : Television
 
-(void) mute;
 
@end
 
</cocoa>

Read the rest of this entry »

Tutorial 2-3: The if Statement

Posted by Henry On April - 22 - 2009 1 COMMENT

Let’s start by opening the “Objects Intro” project from the last tutorial. If you haven’t done so already, I strongly recommend that you add some comments to the code you have written so far which explain what each part does.

Once you have done that, open “Objects Intro.m” and edit it to look like this:

#import <foundation /Foundation.h>
#import "Television.h"
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	int chanChange;
 
	Television *myTV = [[Television alloc] init];
 
	NSLog(@"Type in the channel number:");
	scanf("%i", &chanChange);
 
	[myTV changeChannel: chanChange];
	[myTV watch];
 
    [myTV release];
    [pool drain];
    return 0;
}
</foundation>

Read the rest of this entry »

Tutorial 2-2: Properties and Accessor Methods

Posted by Henry On April - 20 - 2009 1 COMMENT

In this tutorial, we will need to use the project that we started in the last tutorial. So go ahead and open your “Objects Intro” project.

If you remember, last time we created the Television class which we used to create a Television object which could print out two different statements. Today we will add a new method that will change the channel of the television. To do this the television needs to be able to store some data. Let’s go ahead and create a space to store that data now. Open “Television.h” and edit the empty brackets to look like this:

@interface Television : NSObject {
	int channel;
}

We have now got an instance variable in which to store the channel number. An instance variable is data held by an object. For example, if we created two Television objects from the Television class, we could set the channel number of myTV1 to 3, and the channel number of myTV2 to 7. Both Televisions are produced by the Television class, but each can store their own data. But how do we change the channel? This is where Accessor Methods come in.

Read the rest of this entry »

I know you are probably itching to get stuck into the iPhone programming environment, but there are a few things you HAVE to learn first. One of them is Object Oriented Programming. The reason I am jumping right into this is that it is the main feature of Objective-C and a very powerful set of tools which you will need to understand if you want to create iPhone apps.

Let’s start by creating a new project in XCode. Fire up XCode and follow these steps:

  1. In XCode, click File > New Project
  2. Choose Command Line Utility > Foundation Tool
  3. Name the project “Objects Intro” and save it.

In the project window, click the Source folder in the left-hand pane to view your source files. You should have one called “Objects Intro.m” and one called “Objects Intro_Prefix.pch” (which you can ignore for now.)

Object oriented programming languages are based around the use of objects. Objects can represent real life objects (like a chair or a car), or can be more abstract. Objects can hold variables, change variables and pass information between each other. This makes the development of complex applications much more simple. You will understand them more as we continue. Let’s make an object right now!
 

Read the rest of this entry »