ios

59
Scott Leberknight iOS

Upload: scott-leberknight

Post on 22-Jun-2015

2.520 views

Category:

Technology


3 download

DESCRIPTION

Slides for a presentation on iOS that I gave at the fall 2012 Near Infinity conference. Links to the sample applications on GitHub are in the slides.

TRANSCRIPT

Page 1: iOS

Scott Leberknight

iOS

Page 2: iOS

The Big Picture

Patterns

Languages

APIs

Tools

Page 3: iOS

Patterns

MVC(S)

Notifications

Delegation

Protocols

Target-Action

Page 4: iOS

Languages

Objective-C

C(lower-level APIs)

Page 5: iOS

APIsCore

Lots more...

Core GraphicsFoundationUIKit

MapKit

GameKit

Core Location

Core Data Accounts

OpenGL

Social...

Page 6: iOS

XCodeTools

Page 7: iOS

Tools Interface Builder

Page 8: iOS

First App*

MVC

Target-Action

Delegation

http://github.com/sleberknight/fortune-ios-app*

Page 9: iOS

Model

@property (nonatomic, copy) NSArray *fortunes;

FOViewController.h

Page 10: iOS

View

FOViewController.xib

Outlets

Actions

&

Page 11: iOS

Outlets

in XCode...

FOViewController.h

Page 12: iOS

Outlets in IB...

FOViewController.xib

Page 13: iOS

Target-Action

Action - “touch up inside” Target - File’s Owner(our view controller)

Page 14: iOS

Controller

FOViewController.h

#import <UIKit/UIKit.h>

@interface FOViewController : UIViewController

// Model@property (nonatomic, copy) NSArray *fortunes;

// Outlets@property (nonatomic, strong) IBOutlet UITextView *fortuneField;

// Actions- (IBAction)showFortune:(id)sender;

@end

(IBOutlet & IBAction keywords allow

connections in Interface Builder)

Page 15: iOS

Controller

FOViewController.m

#import "FOViewController.h"

@implementation FOViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) { _fortunes = @[ @"A dubious friend may be an enemy in camouflage.", @"A feather in the hand is better than a bird in the air.", @"A friend asks only for your time not your money.", // ... ]; } return self;}

- (IBAction)showFortune:(id)sender { int fortuneIndex = arc4random() % [_fortunes count]; NSLog(@"fortuneIndex: %d", fortuneIndex); NSString *fortune = [_fortunes objectAtIndex:fortuneIndex]; [_fortuneField setText:fortune];}

@end

Page 16: iOS

#import <UIKit/UIKit.h>

// Inherits from UIResponder// Conforms to UIApplicationDelegate protocol

@interface FOAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

Delegation

FOAppDelegate.h

Page 17: iOS

#import "FOAppDelegate.h"#import "FOViewController.h"

@implementation FOAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Create our view controller; set it as the root view controller FOViewController *viewController = [[FOViewController alloc] initWithNibName:@"FOViewController" bundle:nil]; [[self window] setRootViewController:viewController];

self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}

@end

Delegation

FOAppDelegate.m

Page 18: iOS

Run It!

2. Tap “Get Fortune!”

1. Load view controllerin App Delegate

3. showFortune: called

4. Get fortune from model

5. Call setText: on UITextView

Page 19: iOS

Objective-C

Object-oriented(layered on top of C)

Message sending

Single-inheritance

Protocols (like Java interfaces)

Page 20: iOS

Objective-C (continued)

Properties

Selectors

Blocks (like closures)

ARC (automatic reference counting)

GCD (Grand Central Dispatch)

Page 21: iOS

Properties

@property (nonatomic, strong) IBOutlet UITextView *fortuneField;

* Compiler generates:

- instance variable _fortuneField

- getter method fortuneField

- setter method setFortuneField

* IBOutlet indicates it can be an outlet to IB

* Modifiers determine threading and storage model

Page 22: iOS

Sending Messages

[detailViewController setItem:newItem];

receiver

selector

argument

Page 23: iOS

Sending Messages

[self presentViewController:navigationController animated:YES completion:nil];

receiver

selector arguments

Page 24: iOS

Creating Objects

allocate memory

initialize object

WLItemsViewController *itemsViewController = [[WLItemsViewController alloc] init];

Page 25: iOS

Blocks

int amountToAdd = 7;int (^adder)(int) = ^(int num) { return num + amountToAdd;};

NSLog(@"%d", adder(3));// prints "10" to XCode console

Page 26: iOS

[detailViewController setDismissBlock:^{ [[self tableView] reloadData];}];

Blocks - inline

[self tableView] is capturedfrom scope defining block!

Page 27: iOS

Objective-C really is an OK language,once you get used to its quirks,

and it keeps improving...

Page 28: iOS

Core APIs

UIKit

Foundation

Core Graphics

Page 29: iOS

UIKit

“The UIKit framework provides the classes needed to construct and manage an application’s user interface for iOS. It provides an application object, event handling, drawing model, windows, views, and controls specifically designed for a touch screen interface.”

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/_index.html

Page 30: iOS

UIKit & Grand Central Dispatch

"For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way."

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/Introduction/Introduction.html#//apple_ref/doc/uid/TP40006955-CH1-SW1

Page 31: iOS

-(void)handleIncoming:(NSData *)data{ dispatch_async(dispatch_get_main_queue(), ^{

// Retrieve data...

// Update the UI using UIKit methods...

});}

Update UI using GCD

Page 32: iOS

Foundation

“The Foundation framework defines a base layer of Objective-C classes. In addition to providing a set of useful primitive object classes, it introduces several paradigms that define functionality not covered by the Objective-C language.”

http://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/ObjC_classic/_index.html

Page 33: iOS

Core Graphics

“The Core Graphics framework is a C-based API that is based on the Quartz advanced drawing engine. It provides low-level, lightweight 2D rendering with unmatched output fidelity. You use this framework to handle path-based drawing, transformations, color management, offscreen rendering, patterns, gradients and shadings, image data management, image creation, masking, and PDF document creation, display, and parsing.”

http://developer.apple.com/library/ios/#documentation/coregraphics/reference/coregraphics_framework/_index.html

Page 34: iOS

Many, Many More APIs

Core Location MapKit

Quartz Core Animation

AdSupport...Core Motion

Page 35: iOS

Sample Application - Wishlist*

http://github.com/sleberknight/wishlist-ios-app

Displays list of items

Detail view to create/edit items

Move and delete items in list

Choose images for items

*

Page 36: iOS

List of items Editing list

Page 37: iOS

Add/edititem

Chooseimage

Page 38: iOS

Patterns

MVC(S):

Model, View, Controller (, Store)

“Store” is like a DAO (data access object)

Extract store logic from controllers

Page 39: iOS

Patterns

Inheritance - e.g. table view controller

Delegation - e.g. image picker delegate

Notification - e.g. low-memory warning

Page 40: iOS

Model -

#import <Foundation/Foundation.h>

@interface WLItem : NSObject <NSCoding>

-(id)initWithItemName:(NSString *)name;

-(id)initWithItemName:(NSString *)name occasion:(NSString *)occasion store:(NSString *)store price:(int)price;

@property (nonatomic, strong) WLItem *containedItem;@property (nonatomic, weak) WLItem *container;

@property (nonatomic, copy) NSString *itemName;@property (nonatomic, copy) NSString *occasion;@property (nonatomic, copy) NSString *store;@property (nonatomic, assign) int price;@property (nonatomic, copy) NSString *imageKey;@property (readonly, nonatomic, copy) NSDate *dateCreated;@property (nonatomic, copy) NSDate *dateModified;

@end

WLItem

Page 41: iOS

Views

UITableView (presents list of items)

* App delegate creates table view controller programmatically within a navigation controller

* Not defined in XIB

* Edit and + buttons defined in code

Page 42: iOS

Views Detail View Controller

Page 43: iOS

ControllersWLItemsViewController:

Subclass of:

Conforms to: UITableViewDelegateUITableViewDataSource

UITableViewController

Uses UINavigationController to present detail view controller for adding/editing items

Manages moving/deleting items

Responsible for creating table view cells

Page 44: iOS

ControllersWLDetailViewController:

Subclass of:

Conforms to: UITextFieldDelegate

UINavigationControllerDelegate

UIViewController

UIImagePickerControllerDelegate

UIPopoverControllerDelegate

Responsible for loading/saving items & images

Manages image picking controllers

Manages text changed notifications

Page 45: iOS

Stores

WLItemStore

WLImageStore

Stores WLItem objects

Stores UIImage objects

Page 46: iOS

Application Flow

Page 47: iOS

WLAppDelegateapplication:didFinishLaunchingWithOptions

- Create a WLItemsViewController

- Embed WLItemsViewController in UINavigationController

Tap + button, fires addNewItem: which presents detail view controller

- Create a UINavigationController

List editing UI is automatic when add button using editBarButton

WLItemsViewController viewWillAppear: loads table view

Page 48: iOS

viewWillAppear:animated:

viewDidLoad: sets background color

WLDetailViewController init: creates Cancel & Done bar button items

- Sets up form for editing/adding item

- Display image if present

- Register for text change notifications

viewWillDisappear: saves item data

done: dismisses controller, completion handler reloads table data

Page 49: iOS

Tapping camera button invokes takePicture:

UIImagePickerController presents camera or picture library

UIImagePickerController delegate (controller) handles selected image

Page 50: iOS

imagePickerController:didFinishPickingMediaWithInfo retrieves selected image, stores it, and displays it

Use Core Graphics to add a border around image

Use Quartz to add a shadow around image

Page 51: iOS

UITextFieldDelegate method textFieldShouldReturn: dismisses keyboard when return tapped

UIControlEventEditingChanged events trigger selector textChanged: which adds modified date when editing existing items

Page 52: iOS

On iPad use a UIPopoverController for selecting images

Page 53: iOS

UIPopoverController was used to select the delicious image...

Page 54: iOS

One more thing...

Page 55: iOS

Storyboards (an alternative way to define app flow)

Page 56: iOS

Summary

Objective-C

Patterns, Languages, APIs, Tools

MVC(S), Delegation, Protocols

Lots of APIs to use (and learn)

XCode / Interface Builder (IB)

Connect outlets & actions in IB

Page 57: iOS

References

http://www.bignerdranch.com/book/ios_programming_the_big_nerd_ranch_guide_rd_edition_

http://pragprog.com/book/adios/ios-sdk-development

Page 58: iOS

Sample Code

http://github.com/sleberknight/fortune-ios-app

Fortunes:

http://github.com/sleberknight/wishlist-ios-app

Wishlist:

Fortunes:

Page 59: iOS

My Info

scott.leberknight at nearinfinity.com

twitter.com/sleberknight

www.sleberknight.com/blog

www.nearinfinity.com/blogs/scott_leberknight/all/