Development of iPhone Applications

Exclusive offer: get 50% off this eBook here
Grails 1.1 Web Application Development

Grails 1.1 Web Application Development — Save 50%

Reclaiming Productivity for faster Java Web Development

$23.99    $12.00
by Pawan Sachdeva | July 2009 |

In this article by Pawan Sachdeva, we will be introduced to the various features of the iPhone along with basic programming for its development. We will develop a "Hello World" program to highlight its ability to be programmed.

Introduction to iPhone

It has been quite some time since Apple launched iPhone mobile and made a big wave across the world. Initially it got introduced in US market and within a year's time it was available all over the world. Though there are many who use iPhone mobile, I feel it is important to have an introduction to the device in general, its capabilities and what potential it has for programmers to get the full value for the money.

If you have not held an iPhone, I suggest you should do that first. This will motivate you to know more about the device like how Apple implemented it. Some may like the curves, many would like the way it functions, but for me, its sleek, big yet compact form, touch technology and the biggest screen are some of the really cool things.

Development of iPhone Applications

Everyone knows about the "touch technology” of the device, especially the multi-touch which takes care of two fingers touching the screen at the same time at different places. But there is more to iPhone than touch. Other important features are:

  1. Location finding: using GPS it figures out where it is at that point of time in the world
  2. Accelerometer and Orientation: which give it the ability to detect motion in three dimensions
  3. Programmable Vibrator, Camera, address book

Such features make iPhone more than a just a phone. Technically, the iPhone exists in two largely similar versions: the 2007 original release and the 2008 3G release. It is a 4.7- or 4.8-ounce computing device. Each device contains a 620 MHz ARM CPU that has been under clocked to improve battery performance and reduce heat. Both devices include 128 MB of dynamic RAM (DRAM), and from 4 to 16 GB of Flash memory. The primary difference between the two devices centers on the global positioning system (GPS) and networking.

Some technical specifications:

  • iPhone is built on Apple’s OS X, which itself is built on Unix.
  • iPhone has 480 x 320 touch screen
  • Supports a mobile version of Safari browser
  • Supports LAN and WAN networking using Wi-Fi
  • Uses GPS over Wi-Fi

Programming Basics

In 2008, Apple released the iPhone SDK, a developer toolkit that allows programmers to develop iPhone application.

iPhone SDK lets you develop two type of applications—Web development and Native iPhone Application development.

  • Web applications primarily run on a browser using HTML, CSS and some programming language that can generate dynamic content (Java, ASP, .NET, JSP, Ruby….).
  • Native applications run on iPhone like any other application on the device. They use iPhone software development kit (SDK) released by Apple with the inbuilt frameworks and other frameworks that it support.

Ignoring for a while that web and native applications have vast difference in the way they are build and they look, the fact is, they both can produce similar result and for some users it may not be easy to differentiate between them. Interesting isn’t it?

Later in this article we will discuss about native application development using the tools and programming language iPhone supports.

iPhone SDK uses the Objective-C language, which is an extension to the C language. Like any other mobile, iPhone too has a different programming platform; Windows Mobile (VB.NET/C#) and Android (Java) use object-oriented languages and are identical in syntax. However, Objective-C takes a totally different approach. Being different it creates a learning barrier especially for beginners dealing with Mac OS for the first time.

Objective-C as implemented by Apple is built entirely around Objects. It is used throughout the iPhone OS’s frameworks. Windows, views, buttons, sliders and controllers exchange information with each other in the form of events and actions in order to make the program run.

A header file (.h) and a source code (.m) file together represent each object in Objective-C. iPhone OS frameworks provide many standard classes that come with the framework, but sometimes you may have to write your own subclasses. When you do this, you’ll need a new header and source code class together to represent the new subclass in your project.

The iPhone OS is divided in to four layers (Cocoa touch, Media, Core Services, Core OS), as represented in the diagram. Each layer contains variety of frameworks that you can use in your application/program. Initially, you would be dealing with the top layer.

Development of iPhone Applications

  • Cocoa touch - the base framework, that you will deal most of the time. It contains the UI-Kit framework which includes window support, event support and user-interface management.
  • Media – the framework that provide the protocols to deal with audio and video build in iPhone.
  • Core Services – the frameworks used in all applications, data types.
  • Core Operating System – the kernel level software. Dealing with threading, networking, I/O, memory etc…

What you need for iPhone Development

To get started with iPhone programming, you would need the following:

  • An Intel Mac running Mac OS X Leopard v10.5.4 or higher
  • The iPhone SDK for iPhone OS, which you can download from http://developer.apple.com/iphone. The iPhone SDK contains all the tools and utilities you need to develop iPhone applications. In particular, it comes with Xcode 3.1(the development IDE) and the iPhone Simulator that allows you to test your application without needing a real device.

Though, you can test the applications on the simulator, it is a good idea to register yourself on the iPhone developer program with a nominal fee. This registration gets you a secured certificate issued by Apple and can be used to test developed applications on the device itself. Hence, two more things are essential if the application has to be tested on the device

  • iPhone mobile device
  • iPhone develop program registration

If you are serious about taking up iPhone development, I suggest that you to go for the registration process first before anything else. The registration process in itself is free, but to get the certificate you may have to pay a nominal fee ($100 for individuals). All this process could take up quite some time and meanwhile, you can gear up with all the skills and know-how to proceed with the first application. For more information, see "Accessing the iPhone Developer Program Portal.”

The best way to learn iPhone programming is to get your hands dirty and start coding.

To start coding, you’ll need iPhone SDK and XCode IDE that comes along with the SDK. Assuming, you are already done with the installation, you can find XCode application icon in the /developer/applications folder.

Before starting with the application, take a second and think what you would like to name the first application, I will call it "FirstApp". This point is important to keep in mind as the name of the application, subsequent to installation on device, appears on the home screen. The purpose of the application could be anything, but what you want to call it and what you want it to appear as on the home screen starts by naming the application.

Grails 1.1 Web Application Development Reclaiming Productivity for faster Java Web Development
Published: May 2009
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

Create the Project: FirstApp

To create the Hello World project, follow these steps:

  • Launch the Xcode application, located in <Xcode>/Applications.
  • Choose File > New Project.
  • Select the View-Based Application template and click Choose.

    Development of iPhone Applications

  • In the file name test field enter "FirstApp"

XCode would generate some default classes as shown in the following figure:

Development of iPhone Applications

Let’s create a new class file that would do the work for the all time famous "Hello World". From the file menu select new file and select UIView subclass this time and name it HelloWorld.m also select the check below that says "Also create HelloWorld.h" option and click finish.

Development of iPhone Applications

Now, that we have created all the files we need, let touch the code for the first time.

The code used in the article would make more sense to programmers with Objective-C background, however, I have tried to put some comments for programmers with different background to assist them in understanding the code better.

From the file view in XCode(second screenshot), select "FirstApp.m" to open up in the editor pane.

  • Add the below line below the existing "import" line:
    #import "HelloWorld.h"
  • Look for keyword "applicationDidFinishLaunching". Under the comment "// Override point for customization after app launch" add the following lines:
    HelloWorld *myView = [[HelloWorld alloc] 
    initWithFrame:[window frame]];

    [window addSubview:myView];
  • Open the file HelloWorld.m
  • Go to method drawRect and modify it with below code:
    NSString *hello = @"Hello World!";
    CGPoint location = CGPointMake(50,150);
    UIFont *font = [UIFont systemFontOfSize:36];

    [[UIColor redColor] set];

    [hello drawAtPoint:location withFont:font];

Now we are all set to test the first app. Click on the “Build & Go” icon as shown above (second screenshot). Finally, you should see the output on the simulator as below:

Development of iPhone Applications

The complete source code will be as shown below.

FirstApp.m

#import "FirstAppAppDelegate.h"
#import "FirstAppViewController.h"

#import "HelloWorld.h"

@implementation FirstAppAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch
HelloWorld *myView = [[HelloWorld alloc] initWithFrame:[window frame]];

[window addSubview:myView];
[window makeKeyAndVisible];
}


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

@end

HelloWorld.m

#import "HelloWorld.h"

@implementation HelloWorld


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}


- (void)drawRect:(CGRect)rect {
// Drawing code
NSString *hello = @"Hello World!";
CGPoint location = CGPointMake(50,150);
UIFont *font = [UIFont systemFontOfSize:36];

[[UIColor redColor] set];

[hello drawAtPoint:location withFont:font];
}


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

Summary

Thus we saw, Apple created a marvelous device which has GPS that lets the device know the current location, accelerometer that lets the device detect motion, and wide screen for better viewing of data and applications. It uses the Wi-Fi and lets you access the internet like any other laptop or computer. For programmers it is a good opportunity to get into Objective-C and Cocoa Touch technology to explore more and be creative in using the hardware support built in the device. Already there are gaming companies which have jumped into the business by developing these motion sensor devices. You no longer need to press keys to turn the steering of race car; just a tilt does it for you.

References:

http://developer.apple.com/iphone
http://www.iphonedevsdk.com

Grails 1.1 Web Application Development Reclaiming Productivity for faster Java Web Development
Published: May 2009
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Pawan Sachdeva is an IT professional, working with Xoriant Solutions and has over 12 years of experience on Java and related technology and currently is involved in mobile development on Windows Mobile and iPhone.

Books From Packt

Learning jQuery 1.3
Learning jQuery 1.3

Flash with Drupal
Flash with Drupal

Seam 2.x Web Development
Seam 2.x Web Development

Flex 3 with Java
Flex 3 with Java

Django 1.0 Website Development
Django 1.0 Website Development

Apache Struts 2 Web Application Development
Apache Struts 2 Web Application Development

C# 2008 and 2005 Threaded Programming: Beginner's Guide
C# 2008 and 2005 Threaded Programming: Beginner's Guide

PHP and script.aculo.us Web 2.0 Application Interfaces
PHP and script.aculo.us Web 2.0 Application Interfaces

Your rating: None Average: 5 (2 votes)
Best way to develop apps using Windows by
-There actually is a great way to develop iPhone apps on the PC without having to jailbreak your iPhone. Rhomobile has created a hosted development tool called RhoHub that solves this problem and also also gives you some other valuable features like building apps online for all smartphones without having to install SDKs locally. Check it out for free at www.rhohub.com

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
g
F
T
N
e
A
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software