HelloWorld iOS application

Hi folks,

This is very first post about iPhone app development .

I assume you people are familiar with basic of objective c and cocoa framework and Xcode.

So this post bring you the anatomy of first HelloWorld  application

let’s start…

Prerequisites

1. Xcode

2.basic of  objective c and iphone SDK

last and foremost  patience 🙂

Here are the following steps you need to build your HelloWorld application

1.Create new XCode Project

  Create your  project going through XCode ->File ->New Project and select  Empty Application .Name the project HelloWorld and click Save as shown in figure

XCode will create two file AppDelegate.h and AppDelegate.m . Difference between .h and .m file is in objective c all declaration goes to  .h file and implementation goes to .m file.

Now What  is AppDelegate  actually?

When you create your  project, main.m file under  Supporting files is generated by XCode

it looks like

#import <UIKit/UIKit.h>

int main(int argc, char *argv[])

{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    int retVal = UIApplicationMain(argc, argv, nil, nil);

    [pool release];

    return retVal;

}

 as in any C program the execution of Objective -c application starts from main().the main() function starts by creating autorelease pool and ends by releasing it. In between it makes a call to the UiApplicationMain() function. UiApplicationMain() is declared as follows

int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);

The UiApplicationMain() instantiate the application and the application delegate objects. after that,it sets the delegate property of the application object to the application delegate instance.

The Typical application delegate class looks like

#import <UIKit/UIKit.h>

@interface AppDelegate : NSObject <UIApplicationDelegate>

{}

@end

Notice that application delegate class inherits NSObject class and adopts UIApplicationDelegate protocol and implements the UIApplicationDelegate methods.One of the very first  method of UiApplicationDelegate is

 – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{}

This method is starting point for developer didFinishLaunchinWithOptions is application life cycle method it is called when application will launch (onCreate() in Android )

Note : NSObject is the super class of all classes available in objective c and Protocol is  here interface 

 – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

 above code simply illustrate that when application will launch  a new window will be allocated by

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
and  set its background color to white by
self.window.backgroundColor = [UIColor whiteColor];
at last make it visible.

 2. Create User Interface using code 

 For your application very first object you need is appdelegate class which we have been seen above

now suppose you application has more than one screen so how will you create your screen(View)?

In iphone each screen is   ViewController (in Android Activity).

For this create a simple class which is a subclass of UIViewController going through

right click on AppDelegate.h  >>>newFile  >>Cocoa Touch >> UiViewController subclass >>next.

Name the file  and create it.

 3. Create object of your newly created UIViewController and add it to the main Window.

 modify AppDelegate.h file with following piece of code

#import <UIKit/UIKit.h>

#import “HelloWorldViewController.h”

 @interface AppDelegate : UIResponder <UIApplicationDelegate>

{

    HelloWorldViewController *helloWorldVC;

}

@property (strong, nonatomic) UIWindow *window;

@end

modify AppDelegate.m file
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // create viewcontroller object

    helloWorldVC=[[HelloWorldViewController alloc]init];

   //add it to the window

    [self.window addSubview:helloWorldVC.view];

    [self.window makeKeyAndVisible];

    return YES;

}

what you have done so far is you have created  HelloWorldViewController and added it to main window. now create label in your controller

4.Create UiLabel and add it to HelloWorldViewController
  •  Open HelloWorldViewController.h file and declare UILabel
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController
{

    UILabel *label;

}

@end
  •  Open HelloWorldViewController.m file create label and set it’s property

#import “HelloWorldViewController.h”

 @implementation HelloWorldViewController

– (id)init

{

    self = [super init];

    if (self) {

        label=[[UILabel alloc]initWithFrame:CGRectMake(100, 250, 200, 30)];

        label.text=@”HelloWorld”;

    }

    return self;

}

  •  Add label to ViewController

-(void)loadView

{

    [super loadView];

    [self.view addSubview:label];

}

5. Build and Run

Run your project by clicking run button (top left corner in XCode).

 enjoy coding……

Feel free to comment .For any query mail me @ bpsingh@pmtsl.onmicrosoft.com

Advertisements

3 thoughts on “HelloWorld iOS application

  1. To work from home, you must have a setup similar to the following:
    A quiet, comfortable office space. Before you can begin working for CW, you must register
    annd pass an assessment review to qualify. While browsing through the internet, you will come across many websites
    that are specifically devoted to teaching and you can apply for them.

  2. This human touch cannot be duplicated byy the software programs currently available.
    One and only thing to think about here is that lots of the internet web hosting e – Commerce solutions from
    your business hosting business may bbe not what you anticipated.
    If you are into surveys, just select the survey which you
    would like to do, complete it and you will receive your money.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s