Create the first iPhone app


Create your first iPhone application

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

The steps are as follows:

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

XcodeWelcomePage

2. Then select Single View Application

CreateProject

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

NewProjectCreateOptions

#4. Make sure you have selected Automatically apply counting to automatically release out-of-scope resources. Click Next.

5. Select the project directory and choose Create

CreateProjectSelectFolder

6. You will see the page as shown below

XcodeProjectPage

You can set the direction, generation and release at the top of the screen. There is a deployment target, the device supports deployment targets for version 4.3 and above, these are not required, for now just focus on running the application.

7. Select iPhone Simulator in the drop-down menu and run it.

runProject

#8. After successfully running the first application, you will get the output as shown below.

iPhoneSimulator1

Change the background color to have the starting interface builder. Select ViewController.xib. Select the background option on the right, change the color and run.

InterfaceBuilder1

In the above project, the deployment target has been set to iOS6.0 by default and auto layout will be enabled.

To ensure that the application can run normally on iOS4.3 devices, we have modified the deployment target when we started creating the application, but we cannot disable automatic layout. To cancel automatic layout, we need to deselect automatic class Check the box on each nib in the file viewer, which is the xib file.

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

Xcode4Workspace

The File Inspector can be found in the Inspector Selector bar shown above, and Auto Layout can be deselected. When you want to target only iOS6.0 devices, you can use Auto Layout.

Of course, you can also use new features. For example, when adding to iOS6, you can use the passbook function. Now, use iOS4.3 as the deployment target.



Deep look at the first iOS app code

5 different files to generate the app as shown below
  • AppDelegate.h
  • AppDelegate.m
  • ViewController.h
  • ViewController.m
  • ViewController.xib

We use single line comments (//) to explain simplicity Code, important project codes are explained below the code.

AppDelegate.h

// 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 @end
Code description
  • AppDelegate calls UIResponder to handle Ios events.
  • Complete UIApplication commands and provide key application events, such as startup completion, termination, etc.
  • Use the UIWindow object to manage and coordinate various perspectives on the screen of an iOS device. Basic views like other loading views. Usually an application has only one window.
  • UIViewController to handle screen flow

AppDelegate.m

// Imports the class Appdelegate's interfaceimport "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 Description
  • UIApplication is defined here. All methods defined above are application UI calls and do not contain any user-defined methods.
  • The UIWindow object is allocated to hold the application allocation object.
  • UIController as the initial view controller of the window
  • Call makeKeyAndVisible to make the window visible

ViewController.h

#import  // Interface for class ViewController@interface ViewController : UIViewController @end
Code description
  • The ViewController class inherits UIViewController and provides a 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 Description
  • Here two methods are defined in the base class of the UIViewController class
  • Initial Call the installer in viewDidLoad after the view is loaded
  • Call didReceviveMemoryWarning in case of memory warning