iOS
-
Upload
scott-leberknight -
Category
Technology
-
view
2.520 -
download
3
description
Transcript of iOS
![Page 1: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/1.jpg)
Scott Leberknight
iOS
![Page 2: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/2.jpg)
The Big Picture
Patterns
Languages
APIs
Tools
![Page 3: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/3.jpg)
Patterns
MVC(S)
Notifications
Delegation
Protocols
Target-Action
![Page 4: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/4.jpg)
Languages
Objective-C
C(lower-level APIs)
![Page 5: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/5.jpg)
APIsCore
Lots more...
Core GraphicsFoundationUIKit
MapKit
GameKit
Core Location
Core Data Accounts
OpenGL
Social...
![Page 6: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/6.jpg)
XCodeTools
![Page 7: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/7.jpg)
Tools Interface Builder
![Page 8: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/8.jpg)
First App*
MVC
Target-Action
Delegation
http://github.com/sleberknight/fortune-ios-app*
![Page 9: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/9.jpg)
Model
@property (nonatomic, copy) NSArray *fortunes;
FOViewController.h
![Page 10: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/10.jpg)
View
FOViewController.xib
Outlets
Actions
&
![Page 11: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/11.jpg)
Outlets
in XCode...
FOViewController.h
![Page 12: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/12.jpg)
Outlets in IB...
FOViewController.xib
![Page 13: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/13.jpg)
Target-Action
Action - “touch up inside” Target - File’s Owner(our view controller)
![Page 14: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/16.jpg)
#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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/17.jpg)
#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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/19.jpg)
Objective-C
Object-oriented(layered on top of C)
Message sending
Single-inheritance
Protocols (like Java interfaces)
![Page 20: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/20.jpg)
Objective-C (continued)
Properties
Selectors
Blocks (like closures)
ARC (automatic reference counting)
GCD (Grand Central Dispatch)
![Page 21: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/22.jpg)
Sending Messages
[detailViewController setItem:newItem];
receiver
selector
argument
![Page 23: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/23.jpg)
Sending Messages
[self presentViewController:navigationController animated:YES completion:nil];
receiver
selector arguments
![Page 24: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/24.jpg)
Creating Objects
allocate memory
initialize object
WLItemsViewController *itemsViewController = [[WLItemsViewController alloc] init];
![Page 25: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/25.jpg)
Blocks
int amountToAdd = 7;int (^adder)(int) = ^(int num) { return num + amountToAdd;};
NSLog(@"%d", adder(3));// prints "10" to XCode console
![Page 26: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/26.jpg)
[detailViewController setDismissBlock:^{ [[self tableView] reloadData];}];
Blocks - inline
[self tableView] is capturedfrom scope defining block!
![Page 27: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/27.jpg)
Objective-C really is an OK language,once you get used to its quirks,
and it keeps improving...
![Page 28: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/28.jpg)
Core APIs
UIKit
Foundation
Core Graphics
![Page 29: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/29.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/30.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/31.jpg)
-(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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/32.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/33.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/34.jpg)
Many, Many More APIs
Core Location MapKit
Quartz Core Animation
AdSupport...Core Motion
![Page 35: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/35.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/36.jpg)
List of items Editing list
![Page 37: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/37.jpg)
Add/edititem
Chooseimage
![Page 38: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/38.jpg)
Patterns
MVC(S):
Model, View, Controller (, Store)
“Store” is like a DAO (data access object)
Extract store logic from controllers
![Page 39: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/39.jpg)
Patterns
Inheritance - e.g. table view controller
Delegation - e.g. image picker delegate
Notification - e.g. low-memory warning
![Page 40: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/40.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/41.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/42.jpg)
Views Detail View Controller
![Page 43: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/43.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/44.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/45.jpg)
Stores
WLItemStore
WLImageStore
Stores WLItem objects
Stores UIImage objects
![Page 46: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/46.jpg)
Application Flow
![Page 47: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/47.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/48.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/49.jpg)
Tapping camera button invokes takePicture:
UIImagePickerController presents camera or picture library
UIImagePickerController delegate (controller) handles selected image
![Page 50: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/50.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/51.jpg)
UITextFieldDelegate method textFieldShouldReturn: dismisses keyboard when return tapped
UIControlEventEditingChanged events trigger selector textChanged: which adds modified date when editing existing items
![Page 52: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/52.jpg)
On iPad use a UIPopoverController for selecting images
![Page 53: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/53.jpg)
UIPopoverController was used to select the delicious image...
![Page 54: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/54.jpg)
One more thing...
![Page 55: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/55.jpg)
Storyboards (an alternative way to define app flow)
![Page 56: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/56.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/57.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/58.jpg)
Sample Code
http://github.com/sleberknight/fortune-ios-app
Fortunes:
http://github.com/sleberknight/wishlist-ios-app
Wishlist:
Fortunes:
![Page 59: iOS](https://reader035.fdocuments.net/reader035/viewer/2022081404/55878b1ad8b42a51318b4711/html5/thumbnails/59.jpg)
My Info
scott.leberknight at nearinfinity.com
twitter.com/sleberknight
www.sleberknight.com/blog
www.nearinfinity.com/blogs/scott_leberknight/all/