50
iPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone – Walkthrough

By: Hector M Lugo-Cordero, MSSaad A Khan, MS

EEL 6788

1

Page 2: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Agenda

• History• What you will need• Model View Controller• Objective-C• Develop with XCode and Interface Builder• Using the Sensors• App Examples• References

2

Page 3: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

History

• Emerges as a device which provides multimedia, Internet access, and smartphone

• Collaboration with Cingular Wireless (now AT&T)

3

Page 4: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Releases

• Time magazine named it the invention of the year in 2007

• Releases– Original: January 9, 2007 Sale: January 29, 2007– 3 G: July 11, 2008 (3G speed, assisted GPS)– 3GS: June 8, 2009 (video cam and voice control)

4

Page 5: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Availability

5

Page 6: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Units in the world

Year Units sold

2007 1,389,000

2008 11,625,000

2009 20,731,000

2010 8,737,000

Total 42,482,000

6

Page 7: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

HardwareItem Description

Power Original 3.7 V, 1400 mAh3G: 3.7 V, 1150 mAh3GS: 3.7 V, 1219 mAh

CPU Original & 3G: Samsung 32-bit RISC ARM 1176JZ(F)-S v1.0

620 MHz underclocked to 412 MHzPowerVR MBX Lite 3D GPU

3GS: Samsung S5PC100 ARM Cortex-A8

833 MHz underclocked to 600 MHzPowerVR SGX GPU

Storage (Flash) Original: 4, 8, 16 GB3G: 8, 16 GB

3GS: 16, 32GB

7

Page 8: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

HardwareItem Description

Memory Original & 3G: 128 MB eDRAM3GS: 256 MB eDRAM

Display 320 × 480 px, 3.5 in (89 mm), 2:3 aspect ratio, 18-bit (262,144-color) LCD with 163

pixels per inch (ppi)

Input Multi-touch touchscreen display, headset controls, proximity and ambient light

sensors, 3-axis accelerometer3GS also includes: digital compass

Camera Original & 3G: 2.0 megapixels3GS: 3.0 megapixels with video

(VGA at 30 fps)

8

Page 9: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

HardwareItem Description

Connectivity Wi-Fi (802.11b/g), Bluetooth 2.0+EDR, USB 2.0/Dock connector

GSM3G also includes:

A-GPS; HSDPA3GS also supports:

7.2 Mbps HSDPABluetooth 2.1+EDR

Dimensions and Weight Original (135 g): 115 mm (4.5 in) (h)61 mm (2.4 in) (w)

11.6 mm (0.46 in) (d)3G (133 g) & 3GS (135 g):

115.5 mm (4.55 in) (h)62.1 mm (2.44 in) (w)12.3 mm (0.48 in) (d)

9

Page 10: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

What you will need

• MAC computer• IPhone SDK

– Xcode– Interface Builder

• To test– Xcode build in simulator– IPhone and Apple’s developer license

10

Page 11: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone Developer (Phone ID)

11

In Xcode select: Window > Organizer

Page 12: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone Developer (Certificate)

12

Keychain Access (Applications Utilities)

Select 2048 bits and RSA (after continue)Save to disk (remember location)

Page 13: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone Developer (Assistant)

13

Login to http://developer.apple.com/iphone, enter developer portalLaunch Assistant

Page 14: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone Developer (Key Inserts)

14

Identify the app, phone, and enter phone ID (previously acquired with organizer)

Page 15: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone Developer(Install Certificate)

15

• Generate certificate from file generated by Keychain Access• After download the profile and certificate• Install on Xcode by dragging it

Page 16: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone Developer (Deploy)

• Select target device

• Organizer allows to take screen shots

• Console included

16

Page 17: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone OS

• OS for iPhone, iPod Touch, and iPad• Has a status bar above (signal strength, battery

status, time)• A dock station is bellow (for main apps)• Multitasking is available but only for Apple apps• 3rd party apps are closed when Home button is

pressed.• Force quit is available (holding power button

and then home button)

17

Page 18: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone OS (Layers)

18

• Cocoa Touch– User interface functionality, buttons, pickers, scroll bars, etc.– Wrappers to core services

• Media – Core Audio, audio recording/mixing– Open AL, Open GL– Video playback– PDF, JPG, PNG

Page 19: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

iPhone OS (Layers)

19

• Core Services– File Access, Address Book– Threading– Core Location– Net Services, URL Utilities

• Core OS– Security, Sockets– Power Management– Certificates, File System

Page 20: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Model View Controller

20

Page 21: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Model View Controller

• Main Window: – App lives here– Not much is done here

• App Delegate– In charge of adding the view and controller to main

window• App View Controller

– App Logic: the brains of the application (optional)– App View: interface is designed here (GUI available)

21

Page 22: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (History)

• Created early on 1980 by Brad Cox and Tom Love at StepStone

• In 1986 Steve Jobs left Apple and started NeXT– 1988 NeXT licensed Objective-C

• In 1996 Apple buys NeXT– Steve Jobs returns to Apple– Mac OS X is born

• It is object oriented– Layer on top of C

22

Page 23: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Syntax)

• Non object oriented operations identical to C• Object oriented follow the Smalltalk syntax• Methods are known as selectors• Calls are know as message• Example

– C++ call: object->method(arguments)– Objective-C message: [object selector: arguments]

23

Page 24: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Dot Syntax)

• Introduced in Objective-C 2.0• Getter

– int age = [person age];– int age = person.age;

• Setter– [person setAge: 25];– person.age = 25;

• Better example– [[person child] setAge: 1]; person.child.age = 1;

24

Page 25: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Methods)

• A dynamic data exists (i.e. id)• -(id) selectorName: arg1 label2: arg2 label3: ar3

– Args are of the form: (type) name– - denotes it is an instance method– + denotes a class method

• Selectors are of type SEL– Useful to see if a class responds to a method

• if ([obj respondsToSelector:sel]) {[obj performSelector:sel withObject:self];

}

25

Page 26: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (cont.)

• Including/Importing libraries– #include: may cause cycles– #import: includes the file if it is not already included

• Memory management (Reference count)Making objects: Removing objects:

Retain ReleaseAlloc AutoreleaseInit DeallocCopy

26

Page 27: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Autorelease)

• Perfect for returning variables created at a method

• Methods that do not use autorelease– “alloc” and “copy”– We need to retain these objects

• All other methods that return an object include autorelease

• We must follow these conventions

27

Page 28: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Autorelease example)

• - (NSString *)fullName {NSString *result;result = [[NSString alloc] initWithFormat:@“%@ %@”,

firstName, lastName];

return result;}

28

[result release];[result autorelease];

Memory leakageReturns nilOK

Page 29: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Implementation)

• Interfaces (protocols)– Work the same way that in JAVA

• Delegates– Are like protocols– Handle events code

• Classes – .h file (header)– .m file (implementation)

29

Page 30: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Properties)

• Convenient for accessing object attributes• Shortcut for getters/setters• Allow to specify

– Access (e.g. read-only)– Memory management policy (e.g. retain)

• atomic vs nonatomic– nonatomic provides quicker access to the field– atomic is better for when the field will be

read/write multiple times (multi threading)

30

Page 31: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Objective-C (Serialization)

• Known as archiving• Done by overriding write: and read: methods• Example:- (id) write: (TypedStream *) stream {

[super write:stream]; objc_write_types(stream, "i", &person.age);

return self; } - (id) read: (TypedStream *) stream {

[super read:stream]; objc_read_types(stream, "i", &person.age); return self; }

31

Page 32: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

XCode• Replaced Project Builder• Organizes files• Includes an editor• Multiple testing targets• Useful for iPhone, MAC• Red link (app not build)

32

Page 33: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Interface Builder

• Works in collaboration with XCode• Good for developing the App View• Need to add keywords to the code

– IBOutlet translates to nothing– IBAction translates to void

• Organized by category of usage

33

Page 34: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

SDK Demo

34

Page 35: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Using the sensors

• Camera• Microphone• Proximity• Accelerometer• Magnetometer• GPS• Making calls• Sending messages

35

Page 36: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Sensors on devices

36

Page 37: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Camera

• Use UIImagePickerController• Implement UIImagePickerControllerDelegateUIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = pickerDelegate; //can be selfpicker.sourceType = UIImagePickerControllerSourceTypeCamera;

Implement the following method in delegate (called after picture is taken)- (void)imagePickerController:(UIImagePickerController *)picker

didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;

37

Page 38: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Microphone

• Core Audio library (AVAudioRecorder)NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithFloat: 44100.0], AVSampleRateKey, [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey, nil];

NSError *error; recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings

error:&error]; if (recorder) { [recorder prepareToRecord]; recorder.meteringEnabled = YES; [recorder record]; } else NSLog([error description]);

38

Page 39: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Proximity

• Turn them onUIDevice *device = [UIDevice currentDevice];device.proximityMonitoringEnabled = YES;BOOL state = device.proximityState;

• Set notifications[[NSNotificationCenter defaultCenter] addObserver: selfselector: @selector(proximityChanged:)name: @"UIDeviceProximityStateDidChangeNotification"object: device]; …- (void) proximityChanged: (NSNotification *)note { UIDevice *device = [note object]; NSLog(@"In proximity: %i", device.proximityState);}

39

Page 40: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Accelerometer

• Range is [-0.5, 0.5]• Declare a class• Start accelerometer• Take measurements

40

Page 41: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Accelerometer (cont.)@interface AccelController:

UIViewController<UIAccelerometerDelegate> { UIAccelerometer *accelerometer;}@end//start- (void)viewDidLoad { accelerometer = [UIAccelerometer sharedAccelerometer]; accelerometer.updateInterval = 0.1; accelerometer.delegate = self; [super viewDidLoad];}

41

Page 42: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Accelerometer (cont.)- (void)accelerometer:(UIAccelerometer *)meter didAccelerate:(UIAcceleration *)acceleration { float x = acceleration.x; float y = acceleration.y; float z = acceleration.z; float angle = atan2(y, x); ...}

42

Page 43: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Magnetometer

• Core location, but available only for 3GSLocationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;if( locationManager.locationServicesEnabled &&locationManager.headingAvailable) {[locationManager startUpdatingLocation];[locationManager startUpdatingHeading];} else {

...}

43

Page 44: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Magnetometer (Delegate)- (void)locationManager:(CLLocationManager *) manager

didUpdateHeading:(CLHeading *) newHeading {if (newHeading.headingAccuracy > 0) {

CLLocationDirection theHeading =

newHeading.magneticHeading;...

}}//to show calibration panel-(BOOL)locationManagerShouldDisplayHeadingCalibration: (CLLocationManager *)manager { return YES;}

44

Page 45: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

GPS

• Core locationlocationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;if( locationManager.locationServicesEnabled ) {

[locationManager startUpdatingLocation];} else {

...}

locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

45

Page 46: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

GPS (Delegate methods)- (void)locationManager:(CLLocationManager *)manager

didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

if( newLocation != oldLocation ) {…}}

- (void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error {…}

46

Page 47: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Making calls, Emails, and SMS

• Calls, emails, and SMS are treated like URL services (UIApplication.sharedApplication)

[[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"tel://123-456-7890"]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:emailAdress?subject=testMail&body=its test mail."]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:111"]];

47

Page 48: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

App Examples

• Battery monitor• LocateMe• WhichWayIsUp• Accerlerometer

48

Page 49: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

References

• http://developer.apple.com/iphone• http://www.learningiphoneprogramming.

com/• http://mobiforge.com/developing/story/d

eploying-iphone-apps-real-devices• http://www.wikipedia.org

49

Page 50: IPhone – Walkthrough By: Hector M Lugo-Cordero, MS Saad A Khan, MS EEL 6788 1

Questions

50