Compiled Chronicles

A software development blog by Angelo Villegas

iOS: Hello World

iOS is one of the most known programming language today in the mobile industry. The first iPhone was release date back June 2007 and has 350,000+ apps as of January 2011. Certainly, it is the most interesting mobile platform to date, and in the release of iOS SDK 4, things just got better.

Things you need

Before continuing, make sure to note all of this requirements. For starters, you’ll need an Intel-based Mac running Snow Leopard (OS X 10.6.7) and an Xcode 3.2.5 or preferably Xcode 4 (I am using Xcode 4).

To download the SDK, you’ll need to register first in the iOS Developer Program. Apple requires signing up before you’re allowed to download the iOS SDK.

To sign up, go to http://developer.apple.com/devcenter/ios/index.action

As you can see on the picture, download of Xcode is limited to version 4. There are two (2) ways to obtain the IDE, one way is to register as an iOS developer and pay $99/year for the standard program, and $299/year for the enterprise program.

Note: The registration on the site is free, but the iOS Developer Program membership is not. I will not go down into details but for you to test the app on a real device, you need to be a program member.

Another way is through the Mac App Store. Xcode 4 is available for $4.99 free.

Xcode comes with several helpful apps, like checking memory leaks of your app, test audio, an integrated environment to develop and debug OpenGL GPU programs, a visual programming language for processing and rendering data, etc.

Note: If you used Xcode before, you may notice that Interface Builder is now missing. Well, it’s not actually missing rather integrated. Yes, Interface Builder is now integrated with Xcode 4 and you don’t need to open another app just to edit the interface of your iOS app.

New to the alley

Before continuing, you should learn more about the differences of the iOS to other programming languages.

First thing, iOS can only take 1 screen at a time. As you may see, iPhone devices has small screen not like the desktop. If you have a background in mobile development then I think you already know this.

Second, iOS devices specially iPod Touches has a small allocated RAM than desktops. Desktops may be built with a 4 – 16 GB of RAM or maybe more. But iOS devices has limited RAMs. iPhone 4 has a 512 MB of RAM and iPod Touch iOS4 has a 256 MB of RAM.

Third is the accessibility. Most desktop programs pretty much have access to everything. However, iOS seriously restricts what your iOS devices can get to. You can only read and write files to the allocated space for your application, this is called an application’s sandbox. Your application is also constrained, you will not be able to access low-number network ports in iOS, or other things that would typically require root or administrative access on a desktop.

Creating your first app

Most of the programming language (as you may already know), your first program is the Hello World. We will not ruin that tradition so here it is.

First thing to do is create a View-Based Application in Xcode. Open Xcode and click Create a new Xcode project, click View-Based Application and name the Project Hello World.

Once the project is open in Xcode, click the disclosure triangle next to Hello World on the left pane. Click HelloWorldViewController.h and edit the code to look like this:

#import <UIKit/UIKit.h>

@interface HelloWorldViewController : UIViewController {
	UILabel *lbl;
}

@property (nonatomic, retain) IBOutlet UILabel *lbl;

@end

After finishing the code. Click HelloWorldViewController.m and edit the code to look like this:

#import "HelloWorldViewController.h"

@implementation HelloWorldViewController

@synthesize lbl;

#pragma mark - Memory Management
- (void)dealloc
{
    [lbl release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
	[lbl setText: @"Hello World"];
	[super viewDidLoad];
}

- (void)viewDidUnload
{
	[self setLbl: nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Single-click (or double click if you prefer to have a separate editor) HelloWorldViewController.xib file and add a label. If the Object Library is not present (right pane) then type control + option + command + 3 (⌃ ⌥ ⌘ 3) or by navigating to View > Utilities > Object Library. The Object Library is in the lower right part of the editor (lower part of the right pane) :

Once open, search for the Label object and drag it to the interface then on the left pane, there will be 3 buttons available: File’s Owner, First Responder, and View.

Single-click File’s Owner then control + click drag to label and when a pop-up appears labeled Outlet pick lbl.

Save (and close the editor if you are in a separate editor) and test the app by clicking the Run button on the top left corner of Xcode. iPhone simulator should display something like this:

Breaking down the code

You’ve seen the code, now let’s talk what they are and what they do.

HelloWorldViewController.h

First will be the line:

#import <UIKit/UIKit.h>

If you already know C then the #import code may be familiar, if you’re thinking #include then you are right, it’s very familiar with #include because #import is a directive added in Objective-C as an improved version of #include. #import ensures that a file is only ever included once so that you never have a problem with recursive includes.

The UIKit framework provides the classes needed to construct and manage an application’s user interface for iOS. It provides an application object, event handling, drawing model, windows, views, and controls specifically designed for a touch screen interface.

What you did here is to import the UIKit Framework to your current project. All of new projects will have this import by default.

UILabel *lbl;

@property (nonatomic, retain) IBOutlet UILabel *lbl;

Here you created an instance variable as properties, with optional attributes to configure the generation of accessor methods.

IBOutlet is a code to tell Interface Builder that the UILabel *lbl; should be or can be connected to one of the labels that will be placed in the app’s view.

HelloWorldViewController.m

Properties are implemented by way of the @synthesize keyword, which generates getter and setter methods according to the property declaration. Alternately, the @dynamic keyword can be used to indicate that accessor methods will be provided by other means.

@synthesize lbl;

Once synthesized, we created a compiler directive:

#pragma mark - Memory Management

This code tells Xcode to put a separator and group all codes below this line with the header Memory Management.

- (void)dealloc is a memory management method that is called before the app totally quits. You should release all of the instance variables in this method. For this example, we created lbl so the dealloc method should look like this:

- (void)dealloc
{
    [lbl release];
    [super dealloc];
}

Same for the - (void)viewDidUnload, this method is called once the view did unload, meaning once the app closes but not quit. All of the views and other data that can be recreated instantly should be niled here. In this case, we set the object lbl to nil.

- (void)viewDidUnload
{
    [self setLbl: nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

Note: nil and NULL is both different and the same. nil (lowercase) is used with pointer objects and NULL (uppercase) is used in a non-pointer object. In iOS, nil has a lot more use than NULL, so you’ll actually use nil than NULL, more often than not.

Then, the last part is to set the text of the label lbl with the string @"Hello World" inside the method - (void)viewDidLoad.

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [lbl setText: @"Hello World"];
    [super viewDidLoad];
}

The - (void)viewDidLoad method is called when the app launches and the view did load. Meaning the view controller (in our case the HelloWorldViewController), is in the foreground and did load already.

Wrapping things up

Now you have learned your first program in iOS using the traditional Hello World! program. This is just a brief introduction, there are many things you still need to learn so don’t forget to subscribe to my blog’s RSS feed for more tutorials.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *