Building your first 2.x application

Discussion in 'iOS Development' started by gojohnnyboi, Aug 11, 2008.

  1. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Before we start, I will state that this guide is for the very very beginners of those who are confused after updating to 2.x developing. This approach will use the iphone-gcc compiler that runs on the iPhone.


    Step 1 - Downloading iPhone-GCC
    To compile on the iPhone, you must download the native gcc compiler from bigboss' cydia repository. This package can be found with the name iPhone 2.0 Toolchain. arm-apple-darwin9 is the new compiler for 2.0.

    Step 2. Choice A. - Building An Application
    If you choose to build your application from scratch, that is fine. Simply write your code, and then follow the steps in step 3 to make it work on 2.0.

    Step 2. Choice B. - Porting An Application
    This would be the more simple approach. If you had an application from 1.x, then you only have to follow the steps in step 3 to make them compile for 2.x.

    Step 3 - The Code Differences
    There are a few code differences in 2.x, that you must be aware of for compiling your application.

    1. The main function changes
    The UIApplicationMain return has changed a bit, but not much. Instead now you must make the last two arguments a string, which is the name of the app controller. For example:

    OLD:
    Code:
    return UIApplicationMain(argc, argv, [ MyApp class ]);
    NEW:
    Code:
    return UIApplicationMain(argc, argv, @"MyApp", @"MyApp");
    2. The UIWindow and background color
    To set up your UIWindow to NOT crash your app now, you must change the following calls to it like so:

    OLD:
    Code:
    [ window _setHidden: FALSE ];
    [ window makeKey: self ];
    [ window orderFront: self ];
    NEW:
    Code:
    [ window makeKeyAndVisible ];
    As for background color, you can no longer ( in most cases ), use CGColorRef. You must convert to UIColor, which requires getting the SDK headers, and then either patching them or working around them. e.g.

    OLD:
    Code:
    float black[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
    [ window setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), black)]
    NEW:
    Code:
    [ window setBackgroundColor: [ UIColor blackColor ] ];
    3. The compiler and linker flag changes
    There are a few flag changes that you must apply for the compiler to do the job right now.

    Linker Flags:
    Compiler Flags:
    YOU MAY ALSO VIEW SOME OF THE CHANGES AT: http://thebigboss.org/moreinfo/Toolchain2.php

    Example: A working makefile for 2.0
    Code:
    CC=arm-apple-darwin9-gcc
    LD=$(CC)
    LDFLAGS=	-lobjc \
    				-framework UIKit \
    				-framework Foundation \
    				-framework CoreFoundation \
    				-framework CoreGraphics \
    				-L"/usr/lib" \
    				-F"/System/Library/Frameworks" \
    				-F"/System/Library/PrivateFrameworks" \
    				-bind_at_load \
    				-multiply_defined suppress
    CFLAGS=-I/var/include -redefined_supress
    App=Window
    
    all:	$(App)
    
    $(App):	main.o WindowApp.o
    	$(LD) $(LDFLAGS) -o $@ $^
    		ldid -S $(App)
    
    
    %.o:	%.m
    		$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    
    clean:
    		rm -f *.o $(App)
    Step 4 - Compiling the application
    SSH into your device, and copy over your source files. From there, in a terminal, change directories ( cd ) to the location of the source files. Once there, you can type "make" if you have a makefile to use, or manually use gcc to compile. Also, you must sign your finished binary with a tool called "ldid" from saurik. that can be done using ldid -S <binary> and its easier just to add it to the makefile.

    Here's the output you should get when trying to compile the sample window application ( note i used cygwin's ssh command to compile, and scp to copy the files over):

    CompilerExample.jpg

    A Sample Application To Compile

    I suggest trying to compile this application and view it just to get the hang of things. I have updated it today and it should work beautifully. Just install it into a test application and you're good for developing!

    UPDATE ~ Sample Application For 2.0

    main.m ( holds your main function, what the app does when run )
    Code:
    #import <UIKit/UIKit.h>
    #import "SBReset.h"
    
    int main(int argc, char* argv[])
    {
    	NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
    	return UIApplicationMain(argc, argv, @"SBReset", @"SBReset");
    	[ pool release ];
    }
    SBReset.h ( Your header file )
    Code:
    #import <UIKit/UIKit.h>
    #import <stdlib.h>
    
    @interface SBReset : UIApplication 
    {
    	UIWindow* window;
    }
    
    @property(nonatomic, retain) UIWindow* window;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)theApplication;
    
    @end
    SBReset.m ( The main file )
    Code:
    #import "SBReset.h"
    
    @implementation SBReset
    
    @synthesize window;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)theApplication
    {
    	self.window = [ [ [ UIWindow alloc ] initWithFrame:  [ UIScreen mainScreen ].bounds ] autorelease ];
    	system("killall SpringBoard");
    }
    
    @end
    Makefile ( the compiler instructions made easier on you! )
    Code:
    CC=arm-apple-darwin9-gcc
    LD=$(CC)
    LDFLAGS=	-lobjc \
    				-framework Foundation \
    				-framework UIKit \
    				-framework CoreFoundation \
    				-multiply_defined suppress \
    				-redefined_suppress \
    				-L"/usr/lib" \
    				-F"/System/Library/Frameworks" \
    				-F"/System/Library/PrivateFrameworks" \
    				-bind_at_load
    CFLAGS=-I/var/include
    
    AppObjects= 	main.o \
    				SBReset.o
    
    App=SBReset
    
    all:	$(App)
    
    $(App):	$(AppObjects)
    	$(LD) $(LDFLAGS) -o $@ $^
    		ldid -S $(App)
    		cp -pf $(App) /Applications/$(App).app/
    
    %.o:	%.m
    		$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    
    clean:
    		rm -f *.o $(App)
    Have fun!
    -John
  2. gtrplyr

    gtrplyr Member

    Joined:
    Jun 9, 2008
    Messages:
    360
    Likes Received:
    0
    Device:
    iPhone 5 (Black)
    that looks EXTREMLY easy!








    /sarcasm
  3. GrooveMachine

    GrooveMachine Active Member

    Joined:
    Dec 27, 2007
    Messages:
    1,741
    Likes Received:
    11
    Device:
    iPod touch
    Cool! This is really useful having read through about half of NerveGas' book, and trying to find the changes in 2.0 programming.
  4. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    John, your sample application link is dead.
  5. hobofan

    hobofan New Member

    Joined:
    Jun 1, 2008
    Messages:
    68
    Likes Received:
    0
    nice guide, bad thing, that i have to go to sleep now
  6. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    try it now
  7. BadKarma

    BadKarma Banned

    Joined:
    Jul 16, 2008
    Messages:
    4,037
    Likes Received:
    0
    Device:
    iPod touch
    Great Tutorial, If it is one

    Please Register or Log in to view images

  8. piratetom95

    piratetom95 New Member

    Joined:
    Jun 17, 2008
    Messages:
    644
    Likes Received:
    0
    Device:
    iPod touch
    so now if i can download voip from 1.1.4, i can port it?
  9. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    No...
  10. ChuckMez

    ChuckMez New Member

    Joined:
    Jun 1, 2008
    Messages:
    149
    Likes Received:
    0
    Device:
    4G iPod touch
    Whoah... a way to port 1.X apps to 2.X?!?! Quick, someone port iSlsk!

Share This Page