iphone development · 2012-07-21 · iphone development: a brief introduction zachary bedell hpc...

84
iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. [email protected] 1 Saturday, November 7, 2009

Upload: others

Post on 11-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

iPhone Development:A Brief Introduction

Zachary BedellHPC Technologies, Inc.

[email protected]

1Saturday, November 7, 2009

Page 2: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

PROJECT

DATE LOCATION07-NOV-2009 TECH VALLEY CODE CAMP

IPHONE DEVELOPMENTA BRIEF INTRODUCTION

Zachary BedellHPC Technologies, Inc.

[email protected]

2Saturday, November 7, 2009

Page 3: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

ECOSYSTEM OVERVIEWPANTHERS AND TIGERS AND LEOPARDS, SNOW MY!

3Saturday, November 7, 2009

Page 4: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Getting Started(This is just a Simulation)

Intel® based Mac

Leopard or Snow Leopard

Your Immortal Soul (Sign the SDK Agreement)

10GB to burn

Knowledge of Objective-C (not optional)

4Saturday, November 7, 2009

Page 5: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Doing it for Real(Device Deployment)Unmodified iPhone or iPod Touch (any generation)

$99/year entry fee

Your 1st & 2nd born (Distribution & Paid Apps Agreements)

Time: Approval takes weeks to months

Chutzpah: No guarantee Apple will approve, no way to pre-screen ideas.

5Saturday, November 7, 2009

Page 6: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

What you can ExpectRude, negative reviews over simple misunderstandings

Rampant application piracy (40-90%)

If you’re lucky: A modest residual income

If you’re insanely lucky (become next Flight Control): Modest full-time wages for a small team

Quite a lot of fun: One warm glowing email is worth a dozen scathing one-star reviews

Remember with client/server resourcesRemember with client/server resources

6Saturday, November 7, 2009

Page 7: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Today’s Agenda

Walkthrough of Xcode & Interface Builder

Intro to Objective-C Syntax

Important iPhone classes, program flow, event handling, and animation

Tour of a Trivial Application (ATOM Parser)

7Saturday, November 7, 2009

Page 8: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Bonus Points (Time Permitting)

Deeper look at Objective-C syntax & OO features

Static code analysis (CLANG)

Performance profiling (Instruments)

Packaging for Distribution

8Saturday, November 7, 2009

Page 9: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Technologies Used

Objective-C (no Objective-C++ today)

CoreData object persistence API

mogenerator - Xcode plugin for CoreData class generation. http://rentzsch.github.com/mogenerator/

Visually designed GUI (mostly, w/ manual tweaks)

SAX-based XML Parser for ATOM feeds

9Saturday, November 7, 2009

Page 10: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Who IS this guy???

Coding for 24 years

Mac OS X since 2004

iPhone since 2007 (the bad old days)

Moderately successful in the AppStore since opening day (BookShelf)

10Saturday, November 7, 2009

Page 11: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

XCODE & INTERFACE BUILDER WALKTHROUGH

11Saturday, November 7, 2009

Page 12: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Don’t Go Away!

Deep Dive Session coming up next!

12Saturday, November 7, 2009

Page 13: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

PROJECT

DATE LOCATION07-NOV-2009 TECH VALLEY CODE CAMP

IPHONE DEVELOPMENTPART II: DEEP DIVE

Zachary BedellHPC Technologies, Inc.

[email protected]

13Saturday, November 7, 2009

Page 14: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

The Objective-C Language

14Saturday, November 7, 2009

Page 15: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Objective-C

Created by Brad Cox & Tom Love

Based on Smalltalk

Strict superset of C

ABI compatible -- call C from Obj-C “for free”

15Saturday, November 7, 2009

Page 16: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Different from C++

Send messages vs. Call methods

Actual implementation address determined at runtime, not compile time

Receiving object “asked” to do something, can do so how ever it pleases

Method look up slower than C++ virtual call, but aggressive in the runtime caching helps

16Saturday, November 7, 2009

Page 17: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Basic Syntax

17Saturday, November 7, 2009

Page 18: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

1. SYNTAX INTRODUCTION

18Saturday, November 7, 2009

Page 19: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

@interface MyClass : ParentClass { int m_member; id m_objectMember;}+ (void)classMethod;+ (id)objectWithThingy:(id)p_thingy;

- (void)instanceMethod;- (int)countForKind:(NSString*)p_kind;- (NSURL*)urlForItemId:(NSString*)p_id inFeed:(TAFeed*)p_feed;@end

19Saturday, November 7, 2009

Page 20: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

#import “MyClass.h”

@implementation+ (void)classMethod {// ...}

/*! Get URL for the given item. */- (NSURL*)urlForItemId:(NSString*)p_id inFeed:(TAFeed*)p_feed { return nil;}@end

20Saturday, November 7, 2009

Page 21: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Basic Obj-C Syntax

Sending Messages:[target method:param];

#import instead of #include

String literals: @”foo”

21Saturday, November 7, 2009

Page 22: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Sending Messages

[myObject addThing:thing withCategory:cat];

Method name is addThing:withCategory:

Receiver is myObject

Params are thing & cat

Receiver may be nil (0) -- returns zero*

22Saturday, November 7, 2009

Page 23: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Types of Methods

+ means class (static) method

- means instance method

When written, method names include +/- and all the pieces: -urlForItemId:inFeed:

@selector() leaves off +/- (more on that later)

23Saturday, November 7, 2009

Page 24: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

2. PROPERTIES

24Saturday, November 7, 2009

Page 25: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Properties@interface MyClass : NSObject { int noHungarian; NSString *m_thing; id m_dontTouch;}@property(assign) int noHungarian;@property(retain,nonatomic) NSString *thing;@property(readonly) id dontTouch;@end

@implementation MyClass@synthesize noHungarian;@synthesize thing = m_thing, dontTouch = m_dontTouch;@end

MyClass *mc; mc.thing = @”foo”;

25Saturday, November 7, 2009

Page 26: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Properties

@synthesize is optional

Provide some/all of your own accessors if you like:

- (void)setThing:(NSString*)p_thing { /* ... */ }

Can still @synthesize getter (or vice versa)

Naming is everything: property “foo” has:- (void)setFoo:(id)p_foo;- (id)foo; // Note: not getFoo

26Saturday, November 7, 2009

Page 27: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Using Objects

27Saturday, November 7, 2009

Page 28: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

3. OBJECT INIT / CLEANUP

28Saturday, November 7, 2009

Page 29: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Object Instantiation

alloc/initMyClass *foo = [[MyClass alloc] init];

- (id)init { if((self = [super init])) { // Class specific initialization } return self;}

29Saturday, November 7, 2009

Page 30: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Object Instantiation

You MUST:

Call an init in super (usually “designated” init)

Assign result of super init call to “self”

Proceed only if return is non-nil

Be prepared for init to return a different object than alloc.

30Saturday, November 7, 2009

Page 31: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Object Instantiation

You May:

Create as many init’s as you want with different parameters

Create convenience methods to return autoreleased objects

31Saturday, November 7, 2009

Page 32: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Memory Management

32Saturday, November 7, 2009

Page 33: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Memory Management

Hybrid of reference counting & garbage collection

You “own” an object if you alloc, copy, or retain it.

You must release or autorelease all objects you own when done.

Mac has real garbage collection now, but not iPhone.

CLANG can help find leaks @ compile time

Instruments helps at debug time

33Saturday, November 7, 2009

Page 34: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Memory Managementalloc & copy return object w/ refCount==1

release decrements refCount immediately

autorelease dec’s next time flow reaches main event loop

Can “undo” an autorelease with a retain

-dealloc is called on object after refCount reaches zero (how long after is undefined)

MUST call [super dealloc] at end of -dealloc

34Saturday, November 7, 2009

Page 35: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Memory Management

All methods which return objects (except alloc & copy) return autorelease’d objects: refCount==1 now, but autorelease ensures it will reach zero at main run loop

retain objects you want to keep beyond run loop

do nothing, and objects will be dealloc’d at main run loop

35Saturday, November 7, 2009

Page 36: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

NSNumber *ltuae = [NSNumber numberWithInt:42];[ltuae retain];

NSNumber *mine = [[NSNumber alloc] initWithInt:42];

[ltuae release];

return [mine autorelease];

retain/release aren’t really necessary here, but...

autorelease & return the target object for convenience

Memory Management

36Saturday, November 7, 2009

Page 37: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Memory ManagementCreate NSAutoreleasePool’s if you won’t get to main loop for a while:

NSAutoreleasePool *pool = [[NSAutoreleasePool] alloc] init];for(int i=0; i<1000000; i++) { // Do some stuff... if(i % 1000 == 0) { // Do it everyone once in a “while” [pool release]; // All autoreleased objects // free’d here. pool = [[NSAutoreleasePool] alloc] init]; }}[pool release]; pool = nil; // Final cleanup

37Saturday, November 7, 2009

Page 38: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Memory Managementautorelease is nice, but wasteful

Use alloc/init rather than convenience methods when:

In a loop

Performance is critical

Working w/ large objects (get them free’d ASAP!)

Okay: [NSArray arrayWithObjects: @”1”, @”2”, nil];

Better: [[NSArray alloc] initWithObjects: @”1”, @”2”, nil];

38Saturday, November 7, 2009

Page 39: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

EVERY TIME YOU LEAK MEMORY...(PLEASE, THINK OF THE KITTENS!)

http://www.flickr.com/photos/ebola8mystinkymonkey/3732413869 - Used by permission

39Saturday, November 7, 2009

Page 40: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Cocoa Class Library

40Saturday, November 7, 2009

Page 41: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Mutable / Immutable

Most objects are immutable w/ mutable versions

NSString <- Set it only once

NSMutableString <- Change all you want

Better for threading, easier to debug, etc.

41Saturday, November 7, 2009

Page 42: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Collections

NSArray

NSDictionary

NSSet

Mutable versions of all

No generics ☹

42Saturday, November 7, 2009

Page 43: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Other Classes

HTTP requests

File manipulation

XML Parsing (Only SAX on iPhone)

HTML rendering (Safari / WebKit)

Defaults / Preferences

Concurrency - Operation Queue

43Saturday, November 7, 2009

Page 44: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Objective-C iPhone Specifics

44Saturday, November 7, 2009

Page 45: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Startup & Shutdown

main()->NSAppMain()->NIB->AppDelegate-applicationDidFinishLaunching:

-applicationWillResignActive:

-applicationWillTerminate:

45Saturday, November 7, 2009

Page 46: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

View Controllers

Tables, back/forward navigation

Provide a single root UIView which OS displays, hides, animates as necessary

46Saturday, November 7, 2009

Page 47: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

View Controllers - Initialization

-init / -awakeFromNib

-loadView -> -viewDidLoad -> -viewWillAppear: -> -viewDidAppear:

-viewWillDisappear: -> -viewDidDisappear: -> -viewDidUnload

47Saturday, November 7, 2009

Page 48: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

UITableViewController

List of records, possibly grouped into sections

delegate provides controller behavior - responds to edits, taps on row, etc.

datasource provides model behavior - number of sections, number of items in each section, views for the table rows themselves

48Saturday, November 7, 2009

Page 49: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

UINavigationController

Provides forward/back navigations for views

Contains other view controllers - often tables

viewcontrollers are pushed/popped on navigation

Handles animation automatically

49Saturday, November 7, 2009

Page 50: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

UINAVIGATIONCONTROLLER & UITABLEVIEWCONTROLLER DEMO

50Saturday, November 7, 2009

Page 51: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Rotation & Autosizing

UIViewController

-willRotateToInterfaceOrientation:duration:

-willAnimateRotationToInterfaceOrientation:duration:

-didRotateFromInterfaceOrientation:

UIView

.autoresizesSubviews

.autoresizingMask

51Saturday, November 7, 2009

Page 52: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Animation

Most view properties can be animated automatically

[UIView beginAnimations:nil context:nil];myView.frame = CGRectMake(160, 240, 0, 0);[UIView commitAnimations];

52Saturday, November 7, 2009

Page 53: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

ANIMATION DEMO

53Saturday, November 7, 2009

Page 54: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Debugging Tricks

NSZombie

54Saturday, November 7, 2009

Page 55: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Interprocess Communication

application:handleOpenURL:

55Saturday, November 7, 2009

Page 56: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

TOUR OF A TRIVIAL APP

56Saturday, November 7, 2009

Page 57: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Advanced Object Oriented Topics

57Saturday, November 7, 2009

Page 58: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Advanced OO: ProtocolsMultiple inheritance** Of specification, not implementation

Think: Java interfaces

Formal or informal -- compiler enforces formal

Mac OS is mostly informal, iPhone mostly formal

Protocols are “adopted,” not “implemented”

No abstract/virtual methods - can throw exception at runtime if not implemented.

58Saturday, November 7, 2009

Page 59: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

5. PROTOCOLS

59Saturday, November 7, 2009

Page 60: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

@protocol Persistable - (void)save; - (void)load;@end

@interface MyClass : NSObject <Persitable> {}@end

@implementation MyClass- (void)save { /* ... */ }- (void)load { /* ... */ }

60Saturday, November 7, 2009

Page 61: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Advanced OO: Categories

Anyone can add methods to existing classes without source access.

Interfaces on steroids...

61Saturday, November 7, 2009

Page 62: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

6. CATEGORIES

62Saturday, November 7, 2009

Page 63: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

@interface NSString(Hashes) - (NSString*)md5; - (NSString*)sha1;@end

@implementation NSString(Hashes) - (NSString*)md5 { /* ... */ } - (NSString*)sha1 { /* ... */ }

#import “NSString+Hashes.h”// ...NSString *md5 = [@”some string” md5];

63Saturday, November 7, 2009

Page 64: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Threading

NSLock & children (wrappers on pthreads)

@synchronized(object) { ... }

⚠Only touch GUI from main thread

[NSThread isMainThread];

[object performSelectorOnMainThread:@selector(something:) withObject:foo waitUntilDone:NO];

64Saturday, November 7, 2009

Page 65: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Fast Enumeration

NSArray *list = [NSArray arrayWithObjects:@”1”, @”2”, @”3”, nil];for(NSString *item in list) { NSLog(@”%@”, item);}

65Saturday, November 7, 2009

Page 66: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Dynamic Objective-C

66Saturday, November 7, 2009

Page 67: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Selectors

Like function pointers

SEL foo = @selector(someMethod:withArg:);[receiver performSelector:foo withObject:bar withObject:baz];

NSArray *foo = ...;[foo makeObjectsPerformSelector:@selector(saveToDisk)];

67Saturday, November 7, 2009

Page 68: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Key-Value Coding

Dynamic property access

[myObject valueForKey:@”foo”] == myObject.foo

[myObject setValue:@”blue” forKey:@”color”];

Works automatically if you use @property’s

68Saturday, November 7, 2009

Page 69: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

If it Quacks...“Supports” monkey patching, duck punching, etc.

void Swizzle(Class c, SEL orig, SEL new) { Method origMethod = class_getInstanceMethod(c, orig); Method newMethod = class_getInstanceMethod(c, new); if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) { class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(newMethod)); } else { method_exchangeImplementations(origMethod, newMethod); }}

Swizzle([MyClass class], @selector(someMethod:), @selector(myImpl:));

69Saturday, November 7, 2009

Page 70: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

TYPES: STATIC OR DUCK -- YOUR CHOICE

70Saturday, November 7, 2009

Page 71: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

4. STATIC OR DUCK TYPES

71Saturday, November 7, 2009

Page 72: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Static or Dynamic Typesid foo;foo = @”some string”;foo = [NSNumber numberWithInt:42];

NSString *foo;foo = @”some string”;/* This is a compile warning: */foo = [NSNumber numberWithInt:42];

Note: id holds any Objective-C object. typedef NSObject *id; /*(More or less...)*/

72Saturday, November 7, 2009

Page 73: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Static or Dynamic Types

Restrict with protocols:- (void)addPersitable:(id<Persitable>)p_item;

More on Protocols next...

73Saturday, November 7, 2009

Page 74: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Static Code Analysis with CLANG

74Saturday, November 7, 2009

Page 75: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

CLANG STATIC CODE ANALYSIS

75Saturday, November 7, 2009

Page 76: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Performance & Memory Profiling

76Saturday, November 7, 2009

Page 77: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

PROFILING WITH INSTRUMENTS

77Saturday, November 7, 2009

Page 78: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Packaging for Distribution

78Saturday, November 7, 2009

Page 79: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Ad-hoc Distribution

Create an IPA file

Distribute it with your provisioning profile (from Apple)

Install both to authorized devices using iTunes or iPhone Configuration Utility

79Saturday, November 7, 2009

Page 80: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Ad-hoc Distribution

IPA’s are ZIP files:

PayLoad/ iTunesArtwork <-- JPEG, 512px square iTunesMetadata.plist MyBundle.app/ _CodeSignature/ <-- Code signing (Xcode) embedded.mobileprovision <-- Profile MyBundle <-- Binary Default.png resources / etc.

80Saturday, November 7, 2009

Page 81: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

Just ZIP up the .app bundle

Upload through iTunes Connect

Then you wait......

AppStore Distribution

81Saturday, November 7, 2009

Page 82: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

About the ScriptsAssumes using git-svn for revision control:

Git for local development w/ dcommit to svn

SVN for integration builds on a server

Uses SVN rev for versioning & can extract it automatically in both Git & SVN environments

Packages IPA for Ad-Hoc or ZIP for AppStore

Evolved from Mac app & older iPhone SDK - a bit crufty

Many sanity checks may no longer be necessary (but don’t hurt) -- from older SDK bugs

Some Bash, some Perl, some PHP, but it works...82Saturday, November 7, 2009

Page 83: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

Copyright ©2009 HPC Technologies, Inc.

BUILD SCRIPTS

83Saturday, November 7, 2009

Page 84: iPhone Development · 2012-07-21 · iPhone Development: A Brief Introduction Zachary Bedell HPC Technologies, Inc. zac@iphonebookshelf.com Saturday, November 7, 2009 1

The End...Questions?

@[email protected]

iphonebookshelf.com/!tvccSlides & Code at:

84Saturday, November 7, 2009