Creating native iPhone apps
by Lucas Newman
Disclaimer: If you break your iPhone as a result of this talk, I am not going to feel bad.
Jailbreaking your phone
• • •
Download Installer.app iphone.nullriver.com/beta Install Community Sources, then OpenSSH and BSD Subsystem Now you can SFTP into your phone with Transmit!
History of the toolchain
• Project started on berlios.de on July 10th by Patrick Walton • UIKit Hello World was posted on July 28th • Community has exploded recently!
Number of dudes making iPhone apps *
June
July
Aug
Sept
*I totally made this graph up.
Installing the toolchain
It’s easy! Well, easier than it used to be...
• Get the system files for the iPhone - Google for iPhone1,1_1.0.2_1C28_Restore.ipsw - Rename it to .zip and unzip it - The system disk image (694-5298-5.dmg) includes files needed to link applications, so we have to decrypt it...
Installing the toolchain
You are so 31337. No, really, I mean it.
• Decrypt the system disk image - Get vfdecrypt from iphone.natetrue.com/ vfdecrypt.c
gcc -O2 vfdecrypt.c -o vfdecrypt -lcrypto
- Extract the key and use vfdecrypt:
•
strings 009-7698-4.dmg | grep '^[0-9a-fAF]*$' | awk '{ if (length($1) == 72) print; }' ./vfdecrypt -i 694-5298-5.dmg -k [key] -o heavenly.dmg
•
Installing the toolchain
• Mount heavenly.dmg and copy the system files over
sudo mkdir -p /usr/local/arm-apple-darwin/heavenly && sudo cp -Rn [iPhone Volume] /usr/local/arm-apple-darwin/heavenly sudo svn co http://svn.berlios.de/svnroot/repos/iphonebinutils/trunk/include /usr/local/arm-apple-darwin/include sudo ln -s /System/Library/Frameworks/ ApplicationServices.framework/Frameworks/ CoreGraphics.framework/Headers /usr/local/arm-apple-darwin/ include/CoreGraphics sudo ln -s /System/Library/Frameworks/ CoreServices.framework/Frameworks/CFNetwork.framework/ Headers /usr/local/arm-apple-darwin/include/CFNetwork
Oh my god this is the third slide on this how long will it go on this is crazy
-
-
-
Installing the toolchain
Last one, I promise.
- Download and install the package from iphone.natetrue.com/ iPhoneToolchainv05.dmg - Get the Xcode template from lucasnewman.com/ xcodetemplate.zip Now you can make iPhone applications with Xcode!
What now?
I heard the iPhone runs like Cocoa or something.
• Most of the frameworks you are familiar with still exist! - Core Foundation, Foundation, Core Graphics, Core Audio, CFNetwork.... • A few new frameworks for creating applications: UIKit, LayerKit, Celestial, et al. - UIKit is roughly equivalent to AppKit - No interface builder, all interfaces are done in code
Demo
UIKit Primer
So close to AppKit, yet so far.
• UIApplication is the top level application
object (think NSApplication)
• Your app must inherit from it • -applicationDidFinishLaunching: is a good
starting point
• UIView is analogous to NSView • A UIView is backed by an LKLayer, so
animating is much easier than NSView in 10.4
Creating a window
- (void)applicationDidFinishLaunching:(GSEventRef)event; { UIWindow *window = [[UIWindow alloc] initWithFrame: [UIHardware fullScreenApplicationContentRect]]; [window setContentView:[[UIView alloc] initWithFrame: [window bounds]]]; [[window contentView] setBackgroundColor:GSColorCreateColorWithDeviceRGBA(1.0, 0.0, 0.0, 1.0)]; // red background [window orderFront:nil]; [window makeKey:nil]; }
Drawing in a view
The coordinate system is flipped!
- (void)drawRect:(CGRect)rect; { CGContextRef context = UICurrentContext(); CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0); CGContextFillRect(context, rect); }
Animating a view
- (void)animateFrameToSmallerSize; { [UIView beginAnimations:nil]; [UIView setAnimationDuration:1.0]; [self setFrame:CGRectInset([self frame], 20.0, 20.0)]; [UIView endAnimations]; }
Handling input
Tap & swipe the night away
- (void)mouseDown:(GSEventRef)event; { CGRect tapRect = GSEventGetLocationInWindow(event); CGPoint mousePoint = [self convertPoint:tapRect.origin fromView:nil]; ... } - (int)swipe:(UIViewSwipeDirection)swipeDirection withEvent:(GSEventRef)event; { CGRect swipeRect = GSEventGetLocationInWindow(event); ... } - (void)acceleratedInX:(float)x Y:(float)y Z:(float)z;
Demo
Using LayerKit
Transform views in 3D! Hit testing still works!
// add perspective LKTransform transform = LKTransformIdentity; transform.m34 = 1.0 / -420.0; // adjust z distance [[[self superview] _layer] setSublayerTransform:transform]; LKLayer *layer = [self _layer]; [layer setTransform:LKTransformMakeRotation(M_PI / 3.0, 1, 0, 0)];
// animate the flip - (id)actionForLayer:(LKLayer *)layer forKey:(NSString *)key; { if ([key isEqualToString:@"transform"]) { LKBasicAnimation *flipAnimation = [LKBasicAnimation animationWithKeyPath:key]; [flipAnimation setDuration:2.0]; return flipAnimation; } else return [super actionForLayer:layer forKey:key]; }
Bonjour
chatty iphone should get on irc if it wants to talk
#import
• Access to the full CFNetService/ CFNetServiceBrowser API for connecting phones - NSNetService was sadly removed from Foundation on the phone • Write a game that uses locally networked iPhones, why not?
Demo
iPhone Dev Resources
• iPhone Dev Wiki
http://iphone.fiveforty.net/wiki/index.php
• slerp
http://phonedev.tumblr.com
• The Unofficial Apple Weblog
http://www.tuaw.com