Ios First Iphone Application
Now let's create a simple view-based application that runs on the iOS simulator.
The steps are as follows:
1. Open Xcode and select to create a new Xcode project.

2. Then select Single View Application.

3. Next, enter the product name (which is also the app name), organization name, and company identifier.

4. Make sure Automatic Reference Counting is selected to automatically release resources that go out of scope. Click Next.
5. Choose a project directory and click Create.

6. You will see the page shown below.

At the top of the screen, you can configure the orientation, build settings, and deployment target. The deployment target supports devices running iOS 4.3 and later; these settings aren't mandatory right nowβlet's focus on running the app first.
7. In the drop-down menu, select iPhone Simulator and run the project.

8. After successfully running your first app, you'll see the output as shown below.

To change the background color using the Interface Builder, open ViewController.xib. On the right-hand side, select the Background option, change the color, and run the app again.

In the above project, the default deployment target is set to iOS 6.0, and Auto Layout is enabled by default.
To ensure the app runs properly on iOS 4.3 devices, we modified the deployment target when creating the app. However, Auto Layout remains enabled. To disable Auto Layout, uncheck the "Use Auto Layout" checkbox in each nib file (i.e., .xib file) in the File Inspector.
The various parts of the Xcode project IDE are shown below (Apple Xcode 4 User Documentation).

You can find the File Inspector in the inspector selector bar at the top, where you can disable Auto Layout. Use Auto Layout only if your target device supports iOS 6.0 or later.
Of course, you can also take advantage of newer featuresβfor example, Passbook becomes available when upgrading to iOS 6. For now, we're targeting iOS 4.3.
* * *
## Deep Dive into the Code of Your First iOS Application
Five different files make up this application, as follows:
* AppDelegate.h
* AppDelegate.m
* ViewController.h
* ViewController.m
* ViewController.xib
We use single-line comments (//) to explain simple code, with important code explanations provided directly beneath the code.
#### AppDelegate.h
```objc
// Header file providing all UI-related items.
#import
// Forward declaration (used when the class will be defined/imported later).
@class ViewController;
// App delegate interface.
@interface AppDelegate : UIResponder
// Window property.
@property (strong, nonatomic) UIWindow *window;
// View controller property.
@property (strong, nonatomic) ViewController *viewController;
@end
###### Code Explanation
* AppDelegate inherits from UIResponder to handle iOS events.
* It implements the UIApplicationDelegate protocol, handling key app lifecycle events such as startup completion and termination.
* The UIWindow object manages and coordinates views displayed on the iOS device screen, similar to other basic view controllers. Typically, an app has only one window.
* UIViewController handles the flow of screens.
#### AppDelegate.m
```objc
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
// This method indicates successful app launch.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [ initWithFrame:[ bounds]];
// Override point for customization after app launch.
self.viewController = [ initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/* Sent when the app is about to move from active to inactive state. This may occur due to temporary interruptions (such as an incoming call or SMS) or when the user quits the app and it transitions to the background. Use this method to pause ongoing tasks, disable timers, and reduce OpenGL ES frame rates. Games should pause during this phase. */
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/* Use this method to release shared resources, save user data, invalidate timers, and store sufficient app state information to restore the app to its current state if it gets terminated later. If your app 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 background to inactive state; here you can undo many changes made while in the background. */
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/* Resume any paused (or not yet started) tasks while the app was inactive. Optionally refresh the user interface if the app was previously in the background. */
}
- (void)applicationWillTerminate:(UIApplication *)application {
/* Called just before the app shuts down. Save data if necessary. See also applicationDidEnterBackground:. */
}
@end
###### Code Explanation
* Here, the UIApplicationDelegate protocol is implemented. All methods defined above relate to app UI management and do not include any user-defined logic.
* A UIWindow object is allocated to hold the appβs main window.
* A UIViewController serves as the initial root view controller for the window.
* Calling `makeKeyAndVisible` makes the window visible.
#### ViewController.h
```objc
#import
@interface ViewController : UIViewController
@end
###### Code Explanation
* The ViewController class inherits from UIViewController, providing a basic view management model for iOS applications.
#### ViewController.m
```objc
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
;
// Perform any additional setup after loading the view, typically from a nib file.
}
- (void)didReceiveMemoryWarning {
;
// Dispose of any resources that can be recreated.
}
@end
###### Code Explanation
* Two methods are implemented within the base class of UIViewController.
* `viewDidLoad` is called after the initial view is loaded.
* `didReceiveMemoryWarning` is invoked when the system issues a memory warning.
YouTip