March 12, 2008 10

iPhone Hello World

By Levi Senft in iPhone

Last week we finally got the official Apple iPhone SDK. I’ve spent the last few years as a web developer, but ever since I’ve gotten a Mac I’ve wanted to get back to doing some “desktop” application programming. So I’ve messed around with Cocoa a little but haven’t really gotten past the stumbling stages with Cocoa or Objective C. I guess what I’m trying to say is I’m not a Cocoa programmer yet.

After downloading the SDK I immediately started looking for some code samples. After looking at Apple’s hello world app I was definitely intimidated, I’m sure it was great for someone that knows Cocoa or Objective C. So after hacking around for a bit I created my own hello world app using a UITextView component. Like all truly classic hello world programs it does nothing more than outputting “Hello World!”. Since I’m using the UITextView you can tap the text area and edit the text.

To create this app I started by creating an XCode project named helloworld. You won’t need to touch the main.m or main.h files, only the helloworldAppDelegate.m and helloworld.AppDeleage.h files.

First off I’ve added two lines to my header file:

    1 //
    2 //  helloworldAppDelegate.h
    3 //  helloworld
    4 //
    5 //  Created by Levi Senft on 3/12/08.
    6 //
    7
    8 #import <UIKit/UIKit.h>
    9
   10 @class MyView;
   11
   12 @interface helloworldAppDelegate : NSObject {
   13     UIWindow *window;
   14     MyView *contentView;
   15     // Levi: Define textView object
   16     UITextView  *textView;
   17 }
   18
   19 @property (nonatomic, retain) UIWindow *window;
   20 @property (nonatomic, retain) MyView *contentView;
   21 // Levi: Declare textView as a property
   22 @property (nonatomic, retain) UITextView *textView;
   23
   24 @end
   25

Next I’ve added half a dozen lines to helloworldAppDelegate.m:

    1 //
    2 //  helloworldAppDelegate.m
    3 //  helloworld
    4 //
    5 //  Created by Levi Senft on 3/12/08.
    6 //
    7
    8 #import "helloworldAppDelegate.h"
    9 #import "MyView.h"
   10
   11 @implementation helloworldAppDelegate
   12 
   13 @synthesize window;
   14 @synthesize contentView;
   15 // Levi: Tell the compiler to synthesize relevant accessors
   16 @synthesize textView;
   17
   18 - (void)applicationDidFinishLaunching:(UIApplication *)application {
   19     // Create window
   20     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
   21
   22     // Set up content view
   23     self.contentView = [[[MyView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
   24     [window addSubview:contentView];
   25
   26     // Levi: Create the text view.
   27     self.textView = [[[UITextView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease];
   28     [textView setEditable:YES];
   29     [textView setText:@"Hello World"];
   30
   31     // Levi: Add a text view to the content view.
   32     [contentView addSubview:textView];
   33
   34     // Show window
   35     [window makeKeyAndVisible];
   36 }
   37
   38 - (void)dealloc {
   39     // Levi: Release the textView
   40     [textView release];
   41     [contentView release];
   42     [window release];
   43     [super dealloc];
   44 }
   45
   46 @end
   47

That should give a giant white text box with the hello world text that you can edit.

Feel free to point out any Cocoa faux pas in my code. I’d love to know about anything I can do to write better code.

Tags: , , ,

10 Responses to “iPhone Hello World”

  1. Costy says:

    Hello,

    Your code looks greath, but I dont know why doesen´t work.
    I had searched a lot for an exemple how can I use a UITextView and nothing, nothing ….
    So, please, if you can, send me an email with the whole project to see if I can run it.

    thanks a lot,
    Costy

  2. Costy says:

    btw
    my email is sir.costy@gmail.com

  3. Levi Senft says:

    Costy,

    It appears the wordpress is rewriting my quotation marks to the pretty ones. XCode, or any compiler, will recognizes them as characters rather than string delimiters.

    Levi

  4. Varun says:

    What about myView class dear.

    I am new to iPhone development. Can you give me some idea how to start and where can I find some useful getting started material?

    Apple docs don’t even talk about hello world. :(

  5. sgh says:

    Where is the MyView class? I see you import it but where is it? I don’t have it.

  6. Gareth says:

    Hi I also would like to know how we should implement MyView.

    Thanks

  7. krishnan says:

    Hi Levi,

    I want an iPhone App to make a phone call. Is it possible? Most have suggested
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:1-000-0000-"]]; using this way. But is there any that I can make call from my application, with out going to Phone Application.

    Please me help me out of this problem.

Leave a Reply