iOS - First iPhone Application


Creating our First App

Now we are just going to create a simple single view application (a blank app) that just runs on the iOS simulator.

The steps are as follows.

1. Open Xcode and select create a new Xcode project.

2. Then select single view application

3. Then enter product name i.e. the name of the application, organization name and then the company identifier

4. Ensure Use automatic reference counting is selected in order to automatically release the resources allocated once it goes out of scope. Click Next.

5. Select the directory for the project and select create.

6. You will see a screen as follows

In the screen above you will able to select the supported orientations, build and release settings. There is a field deployment target, the device version from which we want to support, lets select 4.3 which is the minimum deployment target allowed now. For now these are not required and let's focus on running the application.

7. Now select iPhone simulator in the drop down near Run button and select run.

8. That's it; you have successfully run your first application. You will get an output as follows

Now let's change the background color, just to have a start with interface builder. Select ViewController.xib. Select background option in the right side, change the color and run.

In the above project, by default the deployment target would have been set to iOS 6.0 and auto layout will be enabled. But to ensure our application to run on devices that run iOS 4.3 onwards, we have already modified the deployment target at the start of creation of this application but we didn't disable auto layout, to disable auto layout we need to deselect the auto layout checkbox in file inspector of each nib i.e the xib files. The various sections of Xcode project IDE are given in the following figure (Courtesy: Apple Xcode 4 User documentation).

File inspector is found in the inspector selector bar as shown above and auto layout can be unchecked there. Auto layout can be used when you want to target only iOS 6 devices. Also you'll be able to use many new features like passbook if you raise the deployment target to iOS 6. For now let's stick to iOS 4.3 as deployment target.

Digging deep into the code of the First iOS application

You will find 5 different files that would have been generated for your application. They are listed as follows.

  • AppDelegate.h

  • AppDelegate.m

  • ViewController.h

  • ViewController.m

  • ViewController.xib

We use these single line comments (//) to give simple code explanations and important items explained below the code.


// Header File that provides all UI related items. 
#import <UIKit/UIKit.h> 
 // Forward declaration (Used when class will be defined /imported in future)
@class ViewController;  

 // Interface for Appdelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>
// Property window 
@property (strong, nonatomic) UIWindow *window; 
 // Property Viewcontroller
@property (strong, nonatomic) ViewController *viewController;
//this marks end of interface 
Important items in code
  • AppDelegate inherits from UIResponder that handles iOS events

  • Implements the delegate methods of UIApplication delegate which provide key application events like finished launching, about to terminate and so on.

  • UIWindow object to manage and co-ordinate the various views on the iOS device screen. It's like the base view over which all other views are loaded. Generally there is only one window for an application.

  • UIViewController to handle the screen flow.


// Imports the class Appdelegate's interface
import "AppDelegate.h" 

// Imports the viewcontroller to be loaded
#import "ViewController.h" 

// Class definition starts here
@implementation AppDelegate 

// Following method intimates us the application launched  successfully 
- (BOOL)application:(UIApplication *)application 
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    self.window = [[UIWindow alloc] initWithFrame:
	[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] 
	 initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;

- (void)applicationWillResignActive:(UIApplication *)application
    /* Sent when the application is about to move from active to inactive state.
    This can occur for certain types of temporary interruptions
    (such as an incoming phone call or SMS message)
    or when the user quits the application and it begins the transition to the 
    background state. Use this method to pause ongoing tasks, disable timers, 
    and throttle down OpenGL ES frame rates. Games should use this method 
    to pause the game.*/

- (void)applicationDidEnterBackground:(UIApplication *)application
    /* Use this method to release shared resources, save user data, invalidate 
    timers, and store enough application state information	to restore your
    application to its current state in case it is terminated later. If your 
    application supports background execution, this method is called instead 
    of applicationWillTerminate: when the user quits.*/

- (void)applicationWillEnterForeground:(UIApplication *)application
    /* Called as part of the transition from the background to the inactive state;
    here you can undo many of the changes made on entering the background.*/

- (void)applicationDidBecomeActive:(UIApplication *)application
    /* Restart any tasks that were paused (or not yet started) while the
    application was inactive. If the application was previously in the background, 
	optionally refresh the user interface.*/

- (void)applicationWillTerminate:(UIApplication *)application
    /* Called when the application is about to terminate. Save data if appropriate.
    See also applicationDidEnterBackground:. */

Important items in code
  • UIApplication delegates defined here. All the methods defined above are UI application delegates and contains no user defined methods.

  • UIWindow object is allocated to hold the application is allocated

  • UIViewController is allocated made as window's initial view controller.

  • To make window visible makeKeyAndVisible method is called.



// Interface for class ViewController
@interface ViewController : UIViewController 

Important items in code
  • The ViewController class inherits the UIViewController which provides the fundamental view management model for the iOS applications.


#import "ViewController.h"

// Category, an extension of ViewController class
@interface ViewController ()


@implementation ViewController  

- (void)viewDidLoad
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

- (void)didReceiveMemoryWarning
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

Important items in code
  • Two methods implemented here which are defined in the base class UIViewController

  • Do initial setup in viewDidLoad which is called after view loads

  • didReceiveMemoryWarning method is called in case of memory warning.