Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++

Basics of Building an iPhone Application

Rate me:
Please Sign up or sign in to vote.
4.36/5 (10 votes)
4 May 2010CPOL4 min read 68.3K   45   17
Basics of building an iPhone application
In this article, you will learn how to create a basic “hello world” application in Apple iPhone / iPod touch. If you are familiar with MAC, it is as easy as writing an application for MAC.

Introduction

Are you a programmer looking for a new challenge? Does the thought of building your very own iPhone app make your heart race and your pulse quicken? Are you familiar with MAC development (XCODE)? If so, then start iPhone Development. It is an easy way to learn iPhone development.

In this article, I am going to explain how easily we can create a basic “hello world” application in Apple iPhone / iPod touch. If you are familiar with MAC, it is as easy as writing an application for MAC.

Background and Environment

Apple had released SDK for iPhone with the support of XCode 3.1.2. To install the SDK, you have to register with Apple to get a AppleID. MAC version 10.5 (Mac Leopard) is the minimum OS version to install iPhone SDK 3.1.2. With this installation, you can write sample programs for iPhone but MAC needs your money $99 to run them on iPhone device. For testing purposes, you can use iPhone simulator. Subscribe with iPhone Developer Program by paying $99 USD to MAC you can start deploy and run your applications on iPhone device. Apple has given easy steps to complete this process so I am not going into details.

Create a New Project

After successful installation of iPhone SDK and XCode, now it's time to create a new project and start a sample application.

Select File > New Project or Apple Key + Shift + N to open the new project menu. Select the Applications item of the iPhone OS section from the menu on the left, and select View Based Application from the icons on the right. Select Choose to enter the project name, I have used HelloWorld in the sample code.

HelloWorld_iPhone/image1.JPG

Classes and Packages

After creating project “HelloWorld”, you can find three important packages along with few more. Classes, Resources and Frameworks.

Classes Package is for classes control classes used in iPhone application, Resources Package is to keep all resources like images, docs etc., Frameworks Package to keep used frameworks for the current project.

There are four files in the Classes package:

  1. HelloWorldAppDelegate.h
  2. HelloWorldAppDelegate.m
  3. HelloWorldViewController.h
  4. HelloWorldViewController.m

HelloWorld_iPhone/image2.JPG

The delegate header file consists of a reference object to UIWindow which is used to control all the UI user interactions from the application. It also manages all the other interface components. It also contains a reference to the view controller (HelloWorldViewController).

The auto generated method applicationDidFinishLaunching in the HelloWorldAppDelegate.m file is invoked when the application has loaded. In this method, we will create an object of HelloWorldViewController and add this object to the UIWindow to make the view visible.

C++
[window addSubview:viewController.view]; 
[window makeKeyAndVisible];  

iPhone SDK is designed based on MVC (Model View Controller) design pattern. So all views always exist with a view controller object. Auto code generator of XCode by default generates the View interface, i.e., HelloWorldViewController.xib file. By double clicking the .XIB file, you can open the view in the Interface Builder (IB). You can check the relation of View and Controller also in the IB.

Now it's time to add controls to our view. There are two ways to add controls to the view, programmatically or using Interface Builder (IB). In this example, I am going to discuss how to add controls programmatically.

Define Controls

Open the HelloWorldViewController.h file and in the interface declaration, define the following variables:

C++
IBOutlet UIView *viewHelloWorld; 	//View to display HelloWorld
IBOutlet UIButton *btnShow; 	//Click this Button to display text "HelloWorld"
IBOutlet UILabel *lblHelloWorld; 	//Label to hold "HelloWorld" Text  

Define a view to add button and label to show “HelloWorld” text. And define a method to handle the click event on the button and define properties to access the UI elements for.

We are defining a view to display and a button and label to go in the view. After the curly braces, add a method declaration to accept the click event of the button and also properties to access the UI elements; myButton, myLabel and viewHelloWorld. Your entire code should look something like this:

C++
#import <UIKit/UIKit.h>

@interface MyHelloWorldViewController : UIViewController 
{
   IBOutlet UIButton * btnShow;
   IBOutlet UILabel * lblHelloWorld;
   IBOutlet UIView * viewHelloWorld;
}
- (IBAction)handleEvent:(id)sender;
 @property (nonatomic, retain) UIButton * btnShow;
 @property (nonatomic, retain) UILabel * lblHelloWorld;
 @property (nonatomic, retain) UIView * viewHelloWorld;
 @end

Synthesize Controls

Synthesize the UI elements to create the getters and setters. After the @implementation line, add the below lines in the HelloWorldViewController.m file:

C++
@synthesize btnShow;
@synthesize lblHelloWorld;
@synthesize viewHelloWorld;

Uncomment the loadView method and add the following lines to the method to create a button and label:

C++
- (void)loadView 
    {
    // create and configure the view
    CGRect cgRct = CGRectMake(0.0, 0.0, 480, 320); //define size and position of view 
    viewHelloWorld = [[UIView alloc] initWithFrame:cgRct];//initialize the view    
    viewHelloWorld.autoresizesSubviews =YES;       //allow it to tweak size of 
						                           //elements in view 
    self.view = viewHelloWorld;            // create a UIButton (UIButtonTypeRoundedRect) 
				                           //and play around with settings

    btnShow = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
    btnShow.frame = CGRectMake(100,100, 100, 50); // size and position of button
    [btnShow setTitle:@"Display" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    btnShow.adjustsImageWhenHighlighted = YES;       
    //Add action handler and set current class as target
    [btnShow addTarget:self action:@selector(action:) 
		forControlEvents:UIControlEventTouchUpInside];
  
    //Display Button
   [self.view addSubview:button];     

   //create a label
   cgRct = CGRectMake(100, 170, 100, 50); //define size and position of label
   lblHelloWorld = [[UILabel alloc] initWithFrame:cgRct];
   lblHelloWorld.text = @"Hello World";
   //Display Label
   [self.view addSubview:lblHelloWorld];
  } 

Build and Go

Click the Build and Go button to check your interface is drawn correctly. It should look something like the screen shot below:

HelloWorld_iPhone/image3.JPG

HelloWorld_iPhone/image4.JPG

Writing Events

UIControlEventTouchUpInside

After defining the events in the current class, we need to define handlers to receive the events. The following method defines the handle to the event. When this event is fired, it will change the label text to “Hello World”.

C++
-(void)action:(id)sender
  {
      label.text =@"Bye World";
  }

Now click the Build and Go button again and click the button. The text of the label should change to "Bye World".

History

  • 4th May, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect articledean.com, conveygreetings.com
United States United States
10+Years of experience in Software Development, Project Management, with proficiency in Development and maintenance
of Mobile Computing, Banking, Commercial, Web –Based and Desktop Applications.

Expertise in C#, .NET, MFC, eVc++, VC++ 6.0, VB 6.0, VBA,J2EE with struts framework, Mobile VB 4.0 (App Forge), eVB, VC-COM, TCP/IP socket Programming with ATL, Visual Studo 2005 & 2008, IIS and SQL-Server 2005, Perl and Shell scripting.

Expertise in RIM BlackBerry JDE, Good understanding of J2EE struts framework, with Oracle J-Developer, IBM Rational Software Architect, and Macro Vision InstallShield 12.

In- depth understanding of C++ TRUESPEECH, PCM, Real Audio, Video Codec’s, Audio Conferencing (VoIP), Video Conferencing in VC++, MFC, OOPS, SDLC, N-Tier Architecture with Client Server Technologies, Web Technologies.

Good Experience on ASP.NET, AJAX, VB.NET, Flash, Win32 API, Oracle 8i and SQL-Server 2005.

Comments and Discussions

 
SuggestionMake it fast ;) Pin
DinoCare8-Jan-13 0:09
DinoCare8-Jan-13 0:09 
GeneralMy vote of 3 Pin
CafedeJamaica4-Jan-13 8:38
professionalCafedeJamaica4-Jan-13 8:38 
GeneralMy vote of 5 Pin
Member 43208449-Jun-12 8:05
Member 43208449-Jun-12 8:05 
QuestionGreat article Pin
Michael Haephrati1-Jun-12 11:26
professionalMichael Haephrati1-Jun-12 11:26 
QuestionCan we Read and Send SMS's from Iphone Connected via PC? Pin
RAJEEV_RSD21-Apr-12 16:32
RAJEEV_RSD21-Apr-12 16:32 
QuestionIBOutlets and no InterfaceBuilder ? Pin
Julien Villers30-Sep-10 23:43
professionalJulien Villers30-Sep-10 23:43 
GeneralIt's Mac, not MAC Pin
Gleb Dolgich10-May-10 23:52
Gleb Dolgich10-May-10 23:52 
GeneralHi... 2 minor points re your (good) basic article... Pin
greghudd4-May-10 9:44
greghudd4-May-10 9:44 
QuestionSo, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
kaschimer4-May-10 9:20
kaschimer4-May-10 9:20 
AnswerRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
delyk4-May-10 12:43
delyk4-May-10 12:43 
GeneralRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
gumi_r@msn.com5-May-10 3:46
gumi_r@msn.com5-May-10 3:46 
GeneralRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
musicm1226-May-10 8:09
professionalmusicm1226-May-10 8:09 
GeneralRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
radioman.lt6-May-10 22:26
radioman.lt6-May-10 22:26 
on android and mobile 6.5 i can make software and deploy on my device immediately without any emulators and paychecks ;}~ Poke tongue | ;-P
d{^__^}b - it's time to fly

GeneralRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
PedroMC11-May-10 1:59
PedroMC11-May-10 1:59 
GeneralRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
Tom The Cat19-Jun-10 15:36
Tom The Cat19-Jun-10 15:36 
GeneralRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
Jacob Dixon5-Jul-10 10:06
Jacob Dixon5-Jul-10 10:06 
GeneralRe: So, not only do you need a Mac to develop iPhone apps, you have to PAY Apple too? Pin
Joel Ivory Johnson12-Jul-10 4:27
professionalJoel Ivory Johnson12-Jul-10 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.