12/11/10

iPhone

• More popular in the media, but fewer of them. Programming • Users much more likely to pay for apps. • Apple has the OS tightly controlled. • Apps use Objective-C (no garbage collection) Tom Chothia • Develop on Mac only Internet Computing Workshop • Need a certification from Apple to run app on a phone • Great tools for the GUI.

Xcode Objective-C

• Apples version of /Netbeans • An extension of C • Object orientated. • Mac only  • Method calls: [object method]

• Very similar to other IDEs • No garbage collection when used for apps:

• Only checks for errors at compile time. – Init, alloc, dealloc, retain

Two Types of File The Objective C Type System

• Header files: • Put NS before most types. – Like Interfaces in Java – E.g. NSString – Lists fields and methods • “UI” for types that relate to the user – Ends in “.h” interface • Method/Message files: • “id” = Object in Java – Contains the code for all the fields and – it is used a lot. methods • Type casting is much harder. – Like Class files in Java.

1 12/11/10

Method Declarations Method Call Syntax

Method declarations have the format: Java myString.subString(startPos,endPos); +/- (output_Type) method_name:(input_type) para2_name: (input2_type) Objective C para2_name: (input3_type) …

[myString substring:startPos to:endPos]; + static - not static See example code

Memory management Property and Synthesize

In Objective C you must explicitly manage You must state the properties of any field you the memory of most objects: want to use outside its class, e.g.,

@property (nonatomic,retain) • When you create an object you must use IBOutlet UILabel *statusText; alloc to allocate some memory. • When you’re finished with an object you must in the header file. The field must be use release to release the memory. “synthesized” in the method file: • You pass pointers to objects (*), not the @synthesize statusText; objects themselves.

iPhone Programming Types for the Interface Builder

• Design the user interface using • Types of fields that link to an Interface “Interface Builder” start with UI, e.g., – UILabel: Label type on the Interface • Write the effect of pressing a in • Types of methods that link to interface Objective C. actions have type IBAction. • The Interface Builder scans your code • Match the buttons, labels etc to the and finds all and types. code using the Interface Builder. IB UI

2 12/11/10

Performance Tools Running Apps on a Phone

has many performance analysis • Apps have to be correctly signed to run tools. on an iPhone. – Apple wants $99 a year to let you sign apps. • One of the hardest parts of Objective C – We can also sign a certificate for you under is avoiding memory leaks. the University Development Program. • Start the process by creating a certificate using Keychain: • The leaks tool will check your code for Keychain Access > Certificate Assistant > memory leaks. Request a … Save to disk.

Parts of a Profile Parts of a Profile

• Your Certificate • App ID: identifies apps. – Generate using keychain access application • Every application must be given an ID. – Identifies you to Apple.

• Hint: put a * in the app ID so you can • Device ID: uniquely identifies a iPhone/ use the same app ID with a number of iPad different programs. – Find it in iTunes.

Provisioning Profile Running on an iPhone

• A Provisioning Profile ties all these • Download it from the website and load it into Xcode.

together. • On the “Get Info” menu set the app to use the profile.

• In the info.plist set the application name. • It allows the named developers to run the named apps on the named devices. • Change the target (top left of main ) from simulator to device.

• All done online at the apple members • Connect your iPhone and run. center: developer.apple.com/membercenter

3 12/11/10

Jail Breaking your iPhone Conclusion

• “Jail breaking” your iPhone, removes iPhone programming: the need for signed apps. • Only support for Mac  • www.jailbreakme.com exploits a • Xcode & Objective C segmentation fault in the pdf handler to remove the certificate check. • Requires memory management • Warning: This could seriously damage • You need certificates from Apple. your phone.

Next Lecture

• Module summary. • Exercise 4. • Some information on term’s modules.

• Friday 12:00 Room 245: drop in sessions on phone programming and Exercise 4.

4