Thursday, December 22, 2011

Hello World in iPhone


Today We are going to learn how to write Hello World application in iPhone.



I assume that you have Intel based Mac OS X with Xcode installed.

You know the good thing is, We will Print Hello Message without writing a single line of Objective c code.

So stay with me, and do it step by step by .. don't forget to drop me a message or comment.

At the end of this Tutorial you can find Complete Source code zip file.

PHASE - I (Create New Project)

So, Let's Fire Xcode!! 



click on  "Create a new Xcode project" option



or 

Go to File --> New --> Project

Now we can see a Pop up Window for selecting our application templet 

So make sure you Single View Application templet as shown in below picture and Go for Next..



In the Next Window we need to put Our Project Details this way

 ------------------------------------------------------------------
| Product Name : HelloWorld                                |
| Organization Name :RDCWorld                        |
| Company Identifier : com.rdcworld                   |
|                                                                                   |
| Class Prefix :  (leave it blank for now)               |
|                                                                                   |
| Devices : iPhone                                                |
 ------------------------------------------------------------------

Note : Don't forget to Make Tick mark on "Use Automatic Reference Counting" option to enable ARC.



Go to Next --> Select the location where you want to store this project --> Create.




Now you can see Xcode default dashboard with our newly created project .



Our project structure look like




PHASE - II (Design UI)

Open ViewController.xib file

1. drag UILabel from object library 

2. double click on it, update name as "Hello World"  as shown below picture



PHASE - III (Run the Application )

Okay wrap it up this application. Save and let's Run it 



you can see Hello World message on output Screen.



Wow!! we did it.



Hey! Tell me something about 'Hello World' How it Works ?

PHASE - IV (How Hello world app works)

1. When we run the application --> it goes to the entry point of the application, that's main.m 

~ ~ ~ ~ ~ ~ ~ ~ ~              Application Startup Point (main.m)          ~ ~ ~ ~ ~ ~ ~ ~ ~  

Open main.m file it looks like 

-----------------------------------------------------------------------------------------------------------------------------------------
//
//  main.m
//  HelloWorld
//
//  Created by RDC on 3/12/13.
//  Copyright (c) 2013 RDC World. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------

-: Tell me about main.m :-

This code nothing but like a C language code, right? yes because we are going to work on Objective C that's Baby of C Language.

Now see in main method we have added AppDelegate class as Main point, so control goes to the didFinishLaunchingWithOptions method of the AppDelegate.m file

~ ~ ~ ~ ~ ~ ~ ~ ~              Application Delegate Class (AppDelegate)          ~ ~ ~ ~ ~ ~ ~ ~ ~  

Open AppDelegate.h file it looks like 

-----------------------------------------------------------------------------------------------------------------------------------------
//
//  AppDelegate.h
//  HelloWorld
//
//  Created by RDC on 3/12/13.
//  Copyright (c) 2013 RDC World. All rights reserved.
//

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@end
-----------------------------------------------------------------------------------------------------------------------------------------

-: Tell me about AppDelegate.h :-

As .h extension says this is the Header file for our application delegate file, so generally we put all the declarations in the header file like 
1. we want any variable or method global  in our application or we want to add Tab Controller , Navigation controller in entire app the we need to declare them here and create object in its .m file.

Now open AppDelegate.m file 

-----------------------------------------------------------------------------------------------------------------------------------------
//
//  AppDelegate.m
//  HelloWorld
//
//  Created by RDC on 3/12/13.
//  Copyright (c) 2013 RDC World. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //create ViewController's object
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    //add ViewController to the window
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

//for now leave rest all life cycle empty methods..

@end
-----------------------------------------------------------------------------------------------------------------------------------------

-: Tell me about AppDelegate.m :-

Now Here is application's Heart, We need to implement all the method declared in header also we define which ViewController is our app's first screen in didFinishLaunchingWithOptions method.
you can see many empty method with comments here all these method are for our application life cycle.

~ ~ ~ ~ ~ ~ ~ ~ ~              Controller Class (ViewController)           ~ ~ ~ ~ ~ ~ ~ ~ ~   

Open  ViewController.h file look like

-----------------------------------------------------------------------------------------------------------------------------------------
//
//  ViewController.h
//  HelloWorld
//
//  Created by RDC on 3/12/13.
//  Copyright (c) 2013 RDC World. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
-----------------------------------------------------------------------------------------------------------------------------------------

-: Tell me about ViewController.h :-

As .h extension says this is the Header file for our application View Controller file, so generally we put all the declarations in the header file like 
1. create instance of any other UIController, UIButton, UILabel.
2. declare all the variable and methods, protocols etc.


Now open our ViewController.m file 

-----------------------------------------------------------------------------------------------------------------------------------------
//
//  ViewController.m
//  HelloWorld
//
//  Created by RDC on 3/12/13.
//  Copyright (c) 2013 RDC World. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];    
}

@end
-----------------------------------------------------------------------------------------------------------------------------------------

-: Tell me about ViewController.m :-

Again We need to implement all the method declared in header in this file, our main coding part is happen in .m files.

Good Next time we will try some UI Items like Button, TextField etc.


You can find complete project source code zip file here : HelloWorld.zip (63.81 KB)

Stay Tune with RDC.. :)


I Would love to here your thoughts !!