Developing for iPhone

Learn how to create and develop iPhone applications from scratch.

Archive for April, 2009

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 »

Tutorial 1-4: More Variables

Posted by Henry On April - 14 - 2009 1 COMMENT

In this tutorial, I will be showing you a couple more ways to use variables.

Open your “Hello World” project from the previous tutorials, and open the “Hello World.m” file to start editing. Edit the program so it looks like this:

#import <foundation /Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	int age;
	int fingers;
 
	age = 99;
	fingers = 10;
 
	NSLog(@"I am %i years old and I have %i fingers!", age, fingers);
 
    [pool drain];
    return 0;
}</foundation>

Read the rest of this entry »

Tutorial 1-3: Introduction to Variables

Posted by Henry On April - 13 - 2009 1 COMMENT

This tutorial assumes you have followed tutorials 1-1 and 1-2, if you haven’t – go and complete these now.

Ok, let’s open our project we made in the previous tutorial. To open a project follow these steps:

  1. Start XCode
  2. Click File > Open then navigate to your project folder. This will be in the directory you saved it to.
  3. In your project folder, click “Hello World.xcodeproj” and click the Open button.
  4. Your project should open in a new window. Another way of opening a project is by navigating to it in finder and double-clicking the .xcodeproject file. If you have had the project open recently, click File > Open Recent Project and look for the project you want in the list.

Now you should have your project open. Double-click “Hello World.m” to open it for editing. Feel free to run it again to remind yourself what the program does. Remember, you can do this by clicking the Build and Go icon.

 

Read the rest of this entry »

Tutorial 1-2: Hello World!

Posted by Henry On April - 12 - 2009 2 COMMENTS

Ok, you should now have installed the iPhone SDK on your mac. If you haven’t, follow the instructions in tutorial 1-1.

We will be using an application called XCode to build our applications, so fire it up now. (You can find it in the directory you installed the SDK to, if you can’t find it, use the search feature in the top-right corner of the screen.) Upon opening XCode, you should be presented with a splash screen welcoming you to XCode. Close this (and uncheck “show at launch” if you don’t want to see it again). Now follow these instructions to get started:

  1. Click File > New Project.
  2. In the left pane of the new window, choose Command Line Utility and then select Foundation Tool in the right pane. Now click the Choose button at the bottom of the window.
  3. You will now be asked to create a project file. This is where all the files you use in this project will be stored. Save it as “Hello World” in a folder you will remember. I saved mine in User/Documents/iPhoneDev/.
  4. You should now see the project window which is split into 3 parts. The pane on the left is used to organize and group all your project files, the top-right pane is for viewing all the files in a particular folder, and the bottom-right pane is used to preview a file’s contents. For now we only want to see any code documents, so click the Source folder in the left-pane.
  5. You should now see two files in the right pane. “Hello World.m” and “Hello World_Prefix.pch”.

Great! All set up and ready to start coding!

Read the rest of this entry »

Tutorial 1-1: The SDK

Posted by Henry On April - 11 - 2009 2 COMMENTS

Mac OSX and iPhone are completely programmed in a language called Objective-C. So it makes sense that iPhone applications are too. This will be the language we will be learning from now on. We will live, breath, eat and sleep Objective-C until it is engraved onto our brains.

I don’t know enough about other programming languages to be able to compare it to anything else, so you will have to use google for that.

Where do I start?

You will need a few things before you can start making millions from that amazing application you are going to make:

  • A Mac. Surely that goes without saying. You do not need to fork out thousands to afford one, a mac-mini would be just fine for our purposes.

Oh wait. That’s it.

Read the rest of this entry »