Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Creating Your First iPhone Application

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.

XcodeWelcomePage

  1. Then select Single View Application

CreateProject

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

NewProjectCreateOptions

  1. 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

CreateProjectSelectFolder

  1. You will see a page as shown below

XcodeProjectPage

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.

  1. Select iPhone Simulator from the drop-down menu and run.

runProject

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

iPhoneSimulator1

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.

InterfaceBuilder1

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)

Xcode4Workspace

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

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.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

ViewController.h

#import  



// 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