Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS 7 Development Crash Course

iOS 7 Development Crash Course

3 hour talk (including live tools & coding demo) gave to Software Engineering University Students on iOS Development for a course in Mobile Development.

The topics cover the technologies needed to tackle a 1 month App project.

* Objective-C rundown (assumes some previous knowledge)
* NSURLSession
* NSURLSessionTask
* NSCoding
* Blocks & Delegation
* CLLocationManager

PS: Re-used slides of my Auto Layout presentation since that one never saw the light out of Hop.in (current employer).

Nicolas Goles

March 20, 2014
Tweet

More Decks by Nicolas Goles

Other Decks in Programming

Transcript

  1. • Objective-C • MVC • Delegation & Blocks • HTTP

    • Persistence • Geolocation • Xcode • Interface Builder CONTENTS
  2. 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
  3. 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?
  4. // Often in Foo.h @interface Foo : NSObject ! @end

    ! // Often in Foo.m @implementation Foo ! @end Class
  5. DELEGATION •Base communication pattern used by the iOS APIs. •Customize

    an object’s behavior and be notified about certain events.
  6. ! // Foo.h @interface Foo : UITableViewController <UIAlertViewDelegate> ! @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:
  7. // // 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:
  8. BLOCKS •“New” to Objective-C* •Can often do what could be

    implemented using delegation.** * iOS 4+ ** Both delegation & blocks have their advantages & requirements.
  9. - (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
  10. - (void)foo { int (^myBlock)(int) = ^(int num) { return

    num * 2; }; int four = myBlock(2); // 2*2 == 4 } eg: BLOCKS
  11. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; config.HTTPAdditionalHeaders = @{@"Accept" : @"text/json"};

    _session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil]; config NSURLSession
  12. 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
  13. 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
  14. 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.
  15. NSCoding @interface Book : NSObject <NSCoding> @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
  16. 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!! }
  17. CLLocationManager * CCLocationManager Reference “The CLLocationManager class defines the interface

    for configuring the delivery of location- and heading-related events to your application.”*
  18. CLLocationManager #import <CoreLocation/CoreLocation.h> ! @interface FooLocation : AController <CLLocationManagerDelegate> @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