Although there’s already a “hello world’ floating out there for the iPhone, it involves a number of complicated classes. I decided to simplify and create a new “Hello World” from scratch based only on UIWindow, UIView and UITextView.
The code follows after the jump.
Makefile
CC=arm-apple-darwin-cc
LD=$(CC)
LDFLAGS=-lobjc -framework CoreFoundation -framework Foundation -framework UIKit -framework LayerKit -framework CoreGraphics
all: SampleApp
SampleApp: mainapp.o SampleApp.o
$(LD) $(LDFLAGS) -v -o $@ $^
%.o: %.m
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
clean:
rm -f *.o SampleApp
mainapp.m
#import <UIKit/UIKit.h>
#import "SampleApp.h"
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
return UIApplicationMain(argc, argv, [SampleApp class]);
}
SampleApp.h
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UITextView.h>
#import <UIKit/UIView.h>
@interface SampleApp : UIApplication {
UIView *mainView;
UITextView *textView;
}
@end
SampleApp.m
#import "SampleApp.h"
@implementation SampleApp
- (void) applicationDidFinishLaunching: (id) unused
{
UIWindow *window;
struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
rect.origin.x = rect.origin.y = 0.0f;
window = [[UIWindow alloc] initWithContentRect: rect];
mainView = [[UIView alloc] initWithFrame: rect];
textView = [[UITextView alloc]
initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[textView setEditable:YES];
[textView setTextSize:14];
[window orderFront: self];
[window makeKey: self];
[window _setHidden: NO];
[window setContentView: mainView];
[mainView addSubview:textView];
[textView setText:@"Hello World"];
}
@end


Cool! The next crucial step is getting the debugger attached to the app running on the iPhone :)
I would love to see start to finish on the "hello world". Start with Get a Mac. Then list the tools needed and go from there. MAC rules!! I am a windows developer :(
Obviously, I'm going to need to learn Objective C.
Shouldn't the pool be released on exit?
Where does UIKit come from?
Awesome!!
There's an error in the make file:
SampleApp: mainapp.o SampleApp.o
should be:
SampleApp: main.o SampleApp.o
Or you could rename main.m to mainapp.m. Either way.
If you can make available a zip archive of all these source files, that would be helpful. Copied and pasted Makefiles don't work because they include spaces rather than tabs, and there may be other problems encountered when copying and pasting code that can be avoided if the sources can be downloaded.
yes, please a 1.2.3 guide from start to finish would help for all us noobs getting into this
This won't compile on my machine. i'm stuck. got 1.1.1 itouch jailbroke, v5 toolchain w/ PATH updated, took hello_src.zip as well as iPhoneHelloWorld.zip tried to compile hello_src (w/ updated makefile) and then the iPhoneHelloWorld (the code above, just from a zipfile - http://www.simplifymedia.com/misc/iPhoneHelloWorld.zip )
Some bits of the make errors:
In file included from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/Files.h:60,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/Components.h:32,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/CarbonCore.h:85,
from /System/Library/Frameworks/CoreServices.framework/Frameworks/AE.framework/Headers/AE.h:20,
from /System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:21,
from /usr/local/arm-apple-darwin/lib/gcc/arm-apple-darwin/4.0.1/../../../../arm-apple-darwin/include/CoreGraphics/CGEvent.h:17,
from /usr/local/arm-apple-darwin/lib/gcc/arm-apple-darwin/4.0.1/../../../../arm-apple-darwin/include/CoreGraphics/CoreGraphics.h:25,
from /usr/local/arm-apple-darwin/lib/gcc/arm-apple-darwin/4.0.1/../../../../arm-apple-darwin/include/UIKit/UIKit.h:3,
from mainapp.m:1:
/usr/include/sys/fcntl.h:418: error: parse error before ‘__DARWIN_ALIAS_C’
/usr/include/sys/fcntl.h:419: error: parse error before ‘__DARWIN_ALIAS_C’
/usr/include/sys/fcntl.h:420: error: parse error before ‘__DARWIN_ALIAS_C’
make: *** [mainapp.o] Error 1
any help appreciated. running 10.5.0 w/ Xcode 2.5 && 3.0 on 1stgen macbook pro.
How can someone program with Objective C when there is Java?
Is like pilling a pineapple with your nails when you have a machine to do it sitting in your kitchen.
Serious: My time is precious and Java simplifies everything. The rest is history and toys for unproductive, poor nerds.
hello
i can't compile too. first i get
i686-apple-darwin9-gcc-4.0.1: =: No such file or directory
and then a lot of errors like
/usr/include/unistd.h:509: error: syntax error before '__DARWIN_ALIAS_C'
any ideas?
regards + thanks in advance,
sebastian.
I can see that you guys had this for long time, anyways, here is the official hello world :)
http://ktundwal.blogspot.com/2008/03/hello-world-app-using-iphone-sdk.html