Slide 1

Slide 1 text

CRASH COURSE iOS 7 DEV the By Nicolas Goles

Slide 2

Slide 2 text

• Objective-C • MVC • Delegation & Blocks • HTTP • Persistence • Geolocation • Xcode • Interface Builder CONTENTS

Slide 3

Slide 3 text

OBJECTIVE-C what’s

Slide 4

Slide 4 text

OBJECTIVE-C •Superset of the C programming language. ! •Adds syntax for object-oriented capabilities. ! •Provides dynamic typing and binding. ! •Defers many responsibilities until runtime. * Objective-C Intro

Slide 5

Slide 5 text

“In Objective-C one does not simply call a method; one sends a message…” And :)

Slide 6

Slide 6 text

POP QUIZ •How to log a message to the console? ! •How do we call a method in Objective-C? ! •How do we declare a Class in Objective-C?

Slide 7

Slide 7 text

NSLog(@“Is there anybody out there?”); console log:

Slide 8

Slide 8 text

[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view2 attribute: NSLayoutAttributeHeight multiplier:0.5 constant:0]; method call:

Slide 9

Slide 9 text

// Often in Foo.h @interface Foo : NSObject ! @end ! // Often in Foo.m @implementation Foo ! @end Class

Slide 10

Slide 10 text

what’s what’s COCOA TOUCH

Slide 11

Slide 11 text

Cocoa Touch Complete Assortment of Frameworks for building iOS Applications. * Cocoa Touch Layer

Slide 12

Slide 12 text

Audio & Video Data Management Graphics User Applications Networking

Slide 13

Slide 13 text

what’s Why MVC

Slide 14

Slide 14 text

MVC Basic Design Pattern to build an iOS App.

Slide 15

Slide 15 text

MVC ! View ! Controller ! Model User Action Updates Notify Notify

Slide 16

Slide 16 text

what’s BLOCKS & DELEGATES let’s start…

Slide 17

Slide 17 text

DELEGATION •Base communication pattern used by the iOS APIs. •Customize an object’s behavior and be notified about certain events.

Slide 18

Slide 18 text

! // Foo.h @interface Foo : UITableViewController ! @end ! // Foo.m @implementation Foo - (void)showAlert { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test Alert" message:@"EHLO :)" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } @end eg:

Slide 19

Slide 19 text

// // Foo.m // ! @implementation Foo ! - (void)showAlert { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test Alert" message:@"EHLO :)" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; // Will show Alert View } ! // Delegate Method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"User pressed Alert’s View button # %d!”, buttonIndex); } ! @end eg:

Slide 20

Slide 20 text

BLocks

Slide 21

Slide 21 text

BLOCKS •“New” to Objective-C* •Can often do what could be implemented using delegation.** * iOS 4+ ** Both delegation & blocks have their advantages & requirements.

Slide 22

Slide 22 text

- (void)foo { int (^aBlock)(int) = ^(int num) { return num *2; }; } Return Value Block Variable Name Block Parameters Block Parameters (with names) Block Definition assigned to var “aBlock”. BLOCKS

Slide 23

Slide 23 text

- (void)foo { int (^myBlock)(int) = ^(int num) { return num * 2; }; int four = myBlock(2); // 2*2 == 4 } eg: BLOCKS

Slide 24

Slide 24 text

what’s HTTP how to…

Slide 25

Slide 25 text

NSURLSession The NSURLSession class and related classes provide an API for downloading content via HTTP.* * iOS 7+

Slide 26

Slide 26 text

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; config.HTTPAdditionalHeaders = @{@"Accept" : @"text/json"}; _session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; config NSURLSession

Slide 27

Slide 27 text

NSURLSessionTask NSURLSessionTask NSURLSessionDataTask NSURLSessionDataTask NSURLSessionUploadTask NSURLSessionDownloadTask

Slide 28

Slide 28 text

NSURL *URL = [NSURL URLWithString:@"http://example.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; ! NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { // ... }]; [task resume]; GET NSURLSessionTask

Slide 29

Slide 29 text

NSURL *URL = [NSURL URLWithString:@"http://example.com/json_service"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; ! NSURLSession *session = [NSURLSession sharedSession]; __block NSDictionary *JSON; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { //Remember to Check for HTTP errors or NSError NSError *parsingError; JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parsingError]; if (!JSON) { NSLog(@"Handle Error %@", [parsingError localizedDescription]); } NSLog(@"%@", JSON[@"Some Key"]); }]; ! [task resume]; JSON NSURLSessionTask

Slide 30

Slide 30 text

what’s PERSIST DATA how to…

Slide 31

Slide 31 text

NSKeyedArchiver Entity Modeling: NO Querying: NO Speed: Slow* Serialization Format: NSData Migrations: Manual * Speed is somewhat relative… but no, NSKeyedArchiver is not the Ferrari of Serialization.

Slide 32

Slide 32 text

NSKeyedArchiver Does the Job: YES Painful to Use: NO * http://nshipster.com/nscoding/

Slide 33

Slide 33 text

NSKeyedArchiver Simple 2 method @protocol -initWithCoder: encodeWithCoder:

Slide 34

Slide 34 text

NSCoding @interface Book : NSObject @property NSString *title; @property NSUInteger pageCount; @property NSSet *categories; @end ! @implementation Book ! // NSCoding - (instancetype)initWithCoder:(NSCoder *)decoder { self = [super init]; if (!self) { return nil; } ! self.title = [decoder decodeObjectForKey:@"title"]; self.pageCount = [decoder decodeIntegerForKey:@"pageCount"]; self.categories = [decoder decodeObjectForKey:@"categories"]; ! return self; } ! - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:self.title forKey:@"title"]; [encoder encodeInteger:self.pageCount forKey:@"pageCount"]; [encoder encodeObject:self.categories forKey:@"categories"]; } ! @end

Slide 35

Slide 35 text

NSCoding - (void)storeBook { Book *book = [[Book alloc] init]; book.title = @"The man in the High Castle"; book.pageCount = 288; book.categories = [[NSSet alloc] initWithArray:@[@"Science Fiction", @"Alternate History"]]; ! NSData *data = [NSKeyedArchiver archivedDataWithRootObject:book]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"theBook"]; // DON'T DO THIS!! }

Slide 36

Slide 36 text

what’s GEO LOCATION where??

Slide 37

Slide 37 text

CLLocationManager * CCLocationManager Reference “The CLLocationManager class defines the interface for configuring the delivery of location- and heading-related events to your application.”*

Slide 38

Slide 38 text

CLLocationManager •Part of the CoreLocation Framework. •Works with the delegation pattern. •Will ask for user permission.

Slide 39

Slide 39 text

CLLocationManager #import ! @interface FooLocation : AController @property (nonatomic, strong) CLLocationManager *locationManager; @end ! @implementation FooLocation ! - (instancetype)init { if (self = [super init]) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.distanceFilter = kCLDistanceFilterNone; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; [_locationManager startUpdatingLocation]; } return self; } ! // Delegate Implementation - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *lastLocation = [locations lastObject]; CLLocationCoordinate2D lastCoordinate = lastLocation.coordinate; NSLog(@"Last [lat, long] - [%f, %f]", lastCoordinate.latitude, lastCoordinate.longitude); } ! @end

Slide 40

Slide 40 text

what’s XCODE Tools?

Slide 41

Slide 41 text

Xcode Demo & The End :)