Easy Tutorial
❮ Ios Auto Layouts Ios Storyboards ❯

Creating the First iPhone Application

Now let's create a simple view application (a blank application) that runs on the iOS simulator.

Here are the steps:

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

  2. Then select a Single View Application.

  3. Next, enter the product name, which is the application name, the organization name, and the company identifier.

  4. Ensure that Automatic Reference Counting is selected to automatically release resources that are out of scope. Click Next.

  5. Choose the project directory and select Create.

  6. You will see a page as shown below.

At the top of the screen, you can set the orientation, build, and release. There is a deployment target, with device support for versions 4.3 and above. These are not mandatory; for now, just focus on running the application.

  1. In the dropdown menu, select the iPhone Simulator and run it.

  2. Successfully running the first application will yield the output as shown below.

Change the background color to create a starting interface builder. Select ViewController.xib. On the right, select the background option, change the color, and run it.

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 normally on iOS 4.3 devices, we have already modified the deployment target when creating the application, but we do not disable Auto Layout. To cancel Auto Layout, we need to uncheck the Auto Layout checkbox in the file inspector for each nib, which is the xib file.

The various parts of the Xcode project IDE are displayed as follows (Apple Xcode 4 User Documentation).

In the inspector selector bar shown above, you can find the file inspector and uncheck Auto Layout. When you want the target to be only iOS 6.0 devices, you can use Auto Layout.

Of course, you can also use new features, such as when adding to iOS 6, you can use the Passbook feature. Now, set the deployment target to iOS 4.3.


Deep Dive into the First iOS Application Code

We use single-line comments (//) to explain simple code, and important project code explanations are below the code.

AppDelegate.h

// Header file that provides all UI-related items.
#import <UIKit/UIKit.h>

// Forward declaration (Used when the class will be defined/imported in the 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 the end of the interface
@end
Code Explanation

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

// The following method informs us that 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

ViewController.h

#import <UIKit/UIKit.h>

// Interface for class ViewController
@interface ViewController : UIViewController 

@end
Code Explanation

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
❮ Ios Auto Layouts Ios Storyboards ❯