You are on page 1of 20

Programming the iPhone

Introduction

Tuesday, February 3, 2009

About Me
Author/Co Author of 13 books on .NET programming Work on Architecture & Research Team (this means I get to play with all the cool new stuff) Presented at Apples WWDC on Cocoa and iPhone Programming

Tuesday, February 3, 2009

iPhone Development Keys


Objective-C XCode and Interface Builder Instruments - Find memory leaks Shark - Analyze live performance UIKit - Cocoa Touch Deploying to Physical Devices

Tuesday, February 3, 2009

Objective-C
Objective-C isnt like C, it IS C Object-Oriented Smalltalk-inspired Dynamic (runtime dispatching vs. compile-time)

Tuesday, February 3, 2009

Core Objective-C Concepts


Message Passing Delegates and Protocols retain / release Objective C 2.0 New property syntax to match modern languages like C# and Java 1.5 Garbage Collected (opt-in) on Leopard, not garbage collected on the phone / touch

Tuesday, February 3, 2009

Message Passing
[(receiver) (message)];
[myView setTransform:CGAffineTransformMakeRotation(angle)];

NSString *niceNumber = [numFormatter stringFromNumber:[NSNumber numberWithInt:(int)numValue]];

Message parameter names are part of the message name: touchesEnded:withEvent:

Tuesday, February 3, 2009

Delegate Pattern
@interface MyViewController : UIViewController <UIAccelerometerDelegate> { } @end [[UIAccelerometer sharedAccelerometer] setDelegate:self];

[[UIAccelerometer sharedAccelerometer] setDelegate:NULL];

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { ... }

Tuesday, February 3, 2009

Retain and Release


(reference counting)
MUST read this document:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/ MemoryMgmt.html Basics: Objects you need to remain available for your code to work: retain Objects youre done with: release Useful but can be abused: autorelease pools. Dont trust the code - use Instruments to monitor reference counts and memory leaks When in doubt, look at SDK samples to see when others are releasing and retaining.
Tuesday, February 3, 2009

Xcode and Interface Builder


Xcode - full-featured IDE for building both Mac and iPhone Applications Also knows Ruby, Python, and Java (Mac only) Code signing for deploying to devices Sits on top of GDB and GCC Interface Builder - UI design and conguration tool GUI properties serialized in NIB/XIB les Many times too generic for real iPhone apps Outlets (IBOutlet) Connect GUI to Code

Tuesday, February 3, 2009

UIKit - Cocoa Touch Core Concepts


The AppDelegate View Hierarchy MVC (Model - View - Controller) Built-in Controllers and Views Animation

Tuesday, February 3, 2009

UIKit
AppDelegate
Main entry point to application Analagous to a graphical void main(...) Holds reference to and initializes rst View Controller Many samples use AppDelegate for global variables Dont do this! Its bad! (Seriously...)

Tuesday, February 3, 2009

UIKit
View Hierarchy
Views are composited First subview added is in back Subsequent subviews added are closer to user (higher layer number/Z index) Using multiple layers of views and various transparency effects, you can create stunning UIs easily.

Tuesday, February 3, 2009

UIKit
MVC
iPhone and Mac development using Xcode strongly encourages (requires, actually) MVC pattern Model: state objects Caution: some samples use db-aware state objects (bad habit!) OS X desktop: Model objects can be Core Data View - UIView and subclasses Controller - View Controllers and subclasses Views render, Controllers act and respond, Models store and expose data.

Tuesday, February 3, 2009

UIKit
Built-in Controllers and Views
UIViewController UINavigationController UITableViewController UITabBarController UIImagePickerController

Tuesday, February 3, 2009

UIKit
Animation (manual)
// create the path for the keyframe animation CGMutablePathRef thePath = CGPathCreateMutable(); CGPathMoveToPoint(thePath,NULL,15.0f,15.f); CGPathAddCurveToPoint(thePath,NULL, 15.f,250.0f, 295.0f,250.0f, 295.0f,15.0f); CAKeyframeAnimation *theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"]; theAnimation.path=thePath; // create an animation group and add the keyframe animation CAAnimationGroup *theGroup = [CAAnimationGroup animation]; theGroup.animations=[NSArray arrayWithObject:theAnimation]; // set the timing function for the group and the animation duration theGroup.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; theGroup.duration=15.0; // release the path CFRelease(thePath); // adding the animation to the target layer causes it // to begin animating [theLayer addAnimation:theGroup forKey:@"animatePosition"];

Tuesday, February 3, 2009

UIKit
Animation (automatic)

myView.frame = CGRectMake(10,10,150,100);

Tuesday, February 3, 2009

Deploying to Physical Devices


Register on Developer Portal ($99 or $299) Get Developer Key Congure App IDs Generate Provisioning Prole Update Xcode w/Provisioning Prole Deploy to Device Extremely error-prone and brittle process. follow instructions carefully!

Tuesday, February 3, 2009

So what kind of apps can I make??


Accelerometer Location (GPS, WiFi Triangulation, DNS) Contacts Calendar Microphone (see: Ocarina) OpenGL ES (2D and 3D!) Sqlite - Onboard relational database Bonjour (discover nearby instances of your app) Networking (Web Services, low-level TCP/IP, UDP)

Tuesday, February 3, 2009

Resources
Apple http://developer.apple.com/iphone https://devforums.apple.com/community/iphone (need SDK account) http://developer.apple.com/iphone/library/documentation/Xcode/Conceptual/xcode_quick_tour_iphoneos/000-Introduction/ chapter_1_section_1.html Books iPhone Developers Cookbook Objective-C 2.0 WWDC Videos (either need to have attended, or can buy for $200) Me Email: alothien@gmail.com Blog: http://dotnetaddict.dotnetdevelopersjournal.com

Tuesday, February 3, 2009

Code Demo
Hello World, but this one doesnt suck.

Tuesday, February 3, 2009

You might also like