Creating Your First iPhone Application
Now let's create a simple view application (a blank application) that runs on the iOS simulator.
The steps are as follows:
1. Open Xcode and select Create a new Xcode project.

- Then select Single View Application

- Next enter the Product Name, which is the application name, the Organization Name, and the Company Identifier.

- Make sure Automatic Reference Counting is selected to automatically release resources that are out of scope. Click Next.
5. Select the project directory and choose Create

- You will see a page as shown below

At the top of the screen you can set orientations, build, and release. There is a Deployment Target; devices support deployment targets of 4.3 and above. These are not mandatory; for now just focus on running the application.
- Select iPhone Simulator from the drop-down menu and run.

- After successfully running the first application, the output obtained is shown below.

Change the background color to have a starting Interface Builder. Select ViewController.xib. Select the Background option on the right, change the color, and run.

In the above project, by default, the deployment target is set to iOS 6.0 and Auto Layout will be enabled.
To ensure the application runs properly on iOS 4.3 devices, we modified the deployment target when starting to create the application, but we did not disable Auto Layout. To turn off Auto Layout, we need to uncheck the Auto Layout checkbox in the File Inspector of each nib, that is, the xib file.
The parts of the Xcode project IDE are shown below (Apple Xcode 4 User Documentation)

The File Inspector can be found in the inspector selector bar shown above, and Auto Layout can be unchecked. Auto Layout can be used when you want to target only iOS 6.0 devices.
Of course, new features can also be used, such as Passbook when targeting iOS 6. Now, Ios 4.3 is used as the deployment target.
A Closer Look at the First IOS Application Code
Five different files are generated for the application, as shown below
- AppDelegate.h
- AppDelegate.m
- ViewController.h
- ViewController.m
- ViewController.xib
We use single-line comments (//) to explain simple code, and important project code is explained below the code.
AppDelegate.h
// Header File that provides all UI related items.
#import
// Forward declaration (Used when class will be defined /imported in future)
@class ViewController;
// Interface for Appdelegate
@interface AppDelegate : UIResponder
// Property window
@property (strong, nonatomic) UIWindow *window;
// Property Viewcontroller
@property (strong, nonatomic) ViewController *viewController;
//this marks end of interface
@end
Code Explanation
- AppDelegate calls UIResponder to handle Ios events.
- Completes the commands of UIApplication, providing key application events such as launch finished, termination, and so on
- A UIWindow object is used on the screen of an iOS device to manage and coordinate various views; it is like the base view for other loaded views. Usually an application has only one window.
- UIViewController handles the screen flow
AppDelegate.m
// 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:. */
}
@end
Code Explanation
- UIApplication is defined here. All the methods defined above are called by the application UI and do not contain any user-defined methods.
- The UIWindow object is allocated to hold the application's allocated objects.
- UIController as the window's initial view controller
- Calling makeKeyAndVisible makes the window visible
ViewController.h
#import
// Interface for class ViewController
@interface ViewController : UIViewController
@end
Code Explanation
- The ViewController class inherits from UIViewController, providing the basic view management model for iOS applications.
ViewController.m
#import "ViewController.h"
// Category, an extension of ViewController class
@interface ViewController ()
@end
@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.
}
@end
Code Explanation
- Here two methods implement those defined in the base class of the UIViewController class
- viewDidLoad is called after the initial view is loaded for setup
- didReceviveMemoryWarning is called in case of a memory warning