This is a full preview of some chapters of Programming iOS 5, by Matt Neuburg. (The other chapters appear as a title and little more.) For the full book, see http://shop.oreilly.com/product/0636920023562.do. The earlier edition was Programming iOS 4 (http://oreilly.com/catalog/0636920010258/). Copyright 2011 Matt Neuburg.

Programming iOS 5

Revision History
Revision 2.0 Dec 23, 2011

Table of Contents

Preface
1. Acknowledgments for the First Edition
2. Notes on the Second Printing
3. Notes on the Second Edition
I. Language
1. Just Enough C
1.1. Compilation, Statements, and Comments
1.2. Variable Declaration, Initialization, and Data Types
1.3. Structs
1.4. Pointers
1.5. Arrays
1.6. Operators
1.7. Flow Control and Conditions
1.8. Functions
1.9. Pointer Parameters and the Address Operator
1.10. Files
1.11. The Standard Library
1.12. More Preprocessor Directives
1.13. Data Type Qualifiers
2. Object-Based Programming
2.1. Objects
2.2. Messages and Methods
2.3. Classes and Instances
2.4. Class Methods
2.5. Instance Variables
2.6. The Object-Based Philosophy
3. Objective-C Objects and Messages
3.1. An Instance Reference Is a Pointer
3.2. Messages and Methods
3.3. Typecasting and the id Type
3.4. Messages as Data Type
3.5. C Functions
3.6. C Struct Pointers
3.7. Blocks
4. Objective-C Classes
4.1. Class and Superclass
4.2. Interface and Implementation
4.3. Header File and Implementation File
4.4. Class Methods
4.5. The Secret Life of Classes
5. Objective-C Instances
5.1. How Instances Are Created
5.2. Polymorphism
5.3. The Keyword self
5.4. The Keyword super
5.5. Instance Variables and Accessors
5.6. Key–Value Coding
5.7. Properties
5.8. How to Write an Initializer
II. IDE
6. Anatomy of an Xcode Project
6.1. New Project
6.2. The Project Window
6.3. The Project File and Its Dependents
6.4. The Target
6.5. From Project to App
7. Nib Management
7.1. A Tour of the Nib-Editing Interface
7.2. Nib Loading and File’s Owner
7.3. Making and Loading a Nib
7.4. Outlet Connections
7.5. Action Connections
7.6. Additional Initialization of Nib-Based Instances
8. Documentation
8.1. The Documentation Window
8.2. Class Documentation Pages
8.3. Sample Code
8.4. Other Resources
9. Life Cycle of a Project
9.1. Choosing a Device Architecture
9.2. Localization
9.3. Editing Your Code
9.4. Navigating Your Code
9.5. Debugging
9.6. Static Analyzer
9.7. Clean
9.8. Running in the Simulator
9.9. Running on a Device
9.10. Device Management
9.11. Version Control
9.12. Instruments
9.13. Distribution
9.14. Ad Hoc Distribution
9.15. Final App Preparations
9.16. Submission to the App Store
III. Cocoa
10. Cocoa Classes
10.1. Subclassing
10.2. Categories
10.3. Protocols
10.4. Optional Methods
10.5. Some Foundation Classes
10.6. The Secret Life of NSObject
11. Cocoa Events
11.1. Reasons for Events
11.2. Subclassing
11.3. Notifications
11.4. Delegation
11.5. Data Sources
11.6. Actions
11.7. The Responder Chain
11.8. Application Lifetime Events
11.9. Swamped by Events
12. Accessors and Memory Management
12.1. Accessors
12.2. Key–Value Coding
12.3. Memory Management
12.4. Properties
13. Data Communication
13.1. Model–View–Controller
13.2. Instance Visibility
13.3. Notifications
13.4. Key–Value Observing
IV. Views
14. Views
14.1. The Window
14.2. Subview and Superview
14.3. Frame
14.4. Bounds and Center
14.5. Layout
14.6. Transform
14.7. Visibility and Opacity
15. Drawing
15.1. UIImage and UIImageView
15.2. Graphics Contexts
15.3. UIImage Drawing
15.4. CGImage Drawing
15.5. CIFilter and CIImage
15.6. Drawing a UIView
15.7. Graphics Context Settings
15.8. Paths and Drawing
15.9. Clipping
15.10. Gradients
15.11. Colors and Patterns
15.12. Graphics Context Transforms
15.13. Shadows
15.14. Points and Pixels
15.15. Content Mode
16. Layers
16.1. View and Layer
16.2. Layers and Sublayers
16.3. Drawing in a Layer
16.4. Transforms
16.5. Shadows, Borders, and More
16.6. Layers and Key–Value Coding
17. Animation
17.1. Drawing, Animation, and Threading
17.2. UIImageView and UIImage Animation
17.3. View Animation
17.4. Implicit Layer Animation
17.5. Core Animation
17.6. Actions
17.7. Emitter Layers
18. Touches
18.1. Touch Events and Views
18.2. Receiving Touches
18.3. Restricting Touches
18.4. Interpreting Touches
18.5. Gesture Recognizers
18.6. Touch Delivery
V. Interface
19. View Controllers
19.1. The View Controller Hierarchy
19.2. View Controller and View Creation
19.3. Rotation
19.4. Presented View Controller
19.5. Tab Bar Controllers
19.6. Navigation Controllers
19.7. Page View Controller
19.8. Container View Controllers
19.9. Storyboards
19.10. View Controller Lifetime Events
19.11. View Controller Memory Management
20. Scroll Views
20.1. Creating a Scroll View
20.2. Scrolling
20.3. Zooming
20.4. Scroll View Delegate
20.5. Scroll View Touches
20.6. Scroll View Performance
21. Table Views
21.1. Table View Cells
21.2. Table View Data
21.3. Table View Selection
21.4. Table View Scrolling and Layout
21.5. Table View Searching
21.6. Table View Editing
21.7. Table View Menus
22. Popovers and Split Views
22.1. Configuring and Displaying a Popover
22.2. Managing a Popover
22.3. Dismissing a Popover
22.4. Popover Segues
22.5. Automatic Popovers
22.6. Split Views
23. Text
23.1. UILabel
23.2. UITextField
23.3. UITextView
23.4. Core Text
24. Web Views
24.1. Loading Content
24.2. Communicating with a Web View
25. Controls and Other Views
25.1. UIActivityIndicatorView
25.2. UIProgressView
25.3. UIPickerView
25.4. UISearchBar
25.5. UIControl
25.6. Bars
25.7. Appearance Proxy
26. Modal Dialogs
26.1. Alert View
26.2. Action Sheet
26.3. Dialog Alternatives
26.4. Local Notifications
VI. Some Frameworks
27. Audio
27.1. System Sounds
27.2. Audio Session
27.3. Audio Player
27.4. Remote Control of Your Sound
27.5. Playing Sound in the Background
27.6. Further Topics in Sound
28. Video
28.1. MPMoviePlayerController
28.2. MPMoviePlayerViewController
28.3. UIVideoEditorController
28.4. An Introduction to AV Foundation Video
29. Music Library
29.1. Exploring the Music Library
29.2. The Music Player
29.3. The Music Picker
30. Photo Library and Image Capture
30.1. UIImagePickerController
30.2. Image Capture With AV Foundation
30.3. The Assets Library Framework
31. Address Book
31.1. Address Book Database
31.2. Address Book Interface
32. Calendar
32.1. Calendar Database
32.2. Calendar Interface
33. Mail
33.1. Mail Message
33.2. SMS Message
34. Maps
34.1. Displaying a Map
34.2. Annotations
34.3. Overlays
35. Sensors
35.1. Location
35.2. Heading
35.3. Acceleration and Attitude
VII. Final Topics
36. Persistent Storage
36.1. The Sandbox
36.2. Basic File Operations
36.3. Saving and Reading Files
36.4. User Defaults
36.5. File Sharing
36.6. Document Types
36.7. Handing Off a Document
36.8. The Document Architecture
36.9. XML
36.10. SQLite
36.11. Image File Formats
37. Basic Networking
37.1. HTTP Requests
37.2. Bonjour
37.3. Push Notifications
37.4. Beyond Basic Networking
38. Threads
38.1. The Main Thread
38.2. Why Threading Is Hard
38.3. Three Ways of Threading
38.4. Threads and App Backgrounding
39. Undo
39.1. The Undo Manager
39.2. The Undo Interface
39.3. The Undo Architecture
40. Epilogue

List of Figures

1.1. Pointers and assignment
1.2. How a large C program is divided into files
2.1. Class and instance
2.2. Instance variables
2.3. A stack
3.1. Two instances end up with pointers to the same third instance
4.1. Browsing the built-in class hierarchy in Xcode 4
5.1. Dragging a button into a view
5.2. The meaning of self
5.3. Class inheritance, overriding, self, and polymorphism
6.1. The project window, on steroids
6.2. The Project navigator
6.3. The Search navigator
6.4. The Debug layout
6.5. Viewing a log
6.6. Telling an assistant pane to display counterparts
6.7. The Project navigator again
6.8. The project folder
6.9. Build phases
6.10. Target build settings
6.11. Configurations
6.12. The scheme editor
6.13. The Scheme pop-up menu
6.14. The built app, in the Finder
6.15. Contents of the app package
6.16. Options when adding a resource to a project
7.1. Editing a nib file
7.2. The dock, expanded
7.3. Dragging a button into a view
7.4. The Empty Window app’s window is empty no longer
7.5. How an outlet provides a reference to a nib-instantiated object
7.6. Editing a nib, with code in the assistant pane
7.7. Connecting an outlet from the Connections inspector
7.8. Connecting an outlet by Control-dragging from the source object
7.9. Connecting an outlet by dragging from the Connections HUD
7.10. Connecting an action from the Connections inspector
7.11. Connecting an action to a method implementation
7.12. Connecting an action and creating a method implemention
8.1. The start of a typical class documentation page
9.1. How a localized strings file is represented in Xcode
9.2. The autocompletion menu
9.3. A warning with a Fix-it suggestion
9.4. A breakpoint
9.5. A disabled breakpoint
9.6. Paused at a breakpoint
9.7. A valid development certificate, as shown in Keychain Access
9.8. Version comparison
9.9. Instruments graphs memory usage over time
9.10. Instruments describes a leak
9.11. A time profile in Instruments
9.12. The time profile starts to make sense
9.13. My code, time-profiled
11.1. The target–action architecture
12.1. An outlet graph with retain
13.1. Model–view–controller
13.2. The global visibility of user defaults
13.3. Posting a notification
14.1. Overlapping views
14.2. A view hierarchy as displayed in the nib
14.3. A subview inset from its superview
14.4. A subview exactly covering its superview
14.5. The superview’s bounds origin has been shifted
14.6. Before autoresizing
14.7. After autoresizing
14.8. A rotation transform
14.9. A scale transform
14.10. Translation, then rotation
14.11. Rotation, then translation
14.12. Rotation, then translation, then inversion of the rotation
14.13. Skew (shear)
15.1. Mars appears in my interface
15.2. A tiled image of Mars
15.3. A stretched image of Mars
15.4. Two images of Mars combined side by side
15.5. Two images of Mars in different sizes, composited
15.6. Half the original image of Mars
15.7. Image of Mars split in half
15.8. A photo of me, filtered
15.9. The very strange behavior of CGContextClearRect
15.10. A simple path drawing
15.11. Drawing with a gradient
15.12. A patterned fill
15.13. Drawing rotated with a CTM
15.14. Drawing with a shadow
15.15. Automatic stretching of content
16.1. A compass, composed of layers
16.2. A hierarchy of views and the hierarchy of layers underlying it
16.3. Layers that have sublayers of their own
16.4. A view and a layer delegate that draws into it
16.5. One way of resizing the compass arrow
16.6. A gradient drawn behind the compass
16.7. An anchor point plus a vector defines a rotation plane
16.8. A disappointing page-turn rotation
16.9. A dramatic page-turn rotation
16.10. Page-turn rotation applied to a CATransformLayer
16.11. A layer with a mask
17.1. An ease-in-out Bézier curve
17.2. A boat and the course she’ll sail
17.3. A push transition
17.4. Another push transition
17.5. A really boring emitter layer
17.6. An emitter layer that makes a sort of waterfall
17.7. The waterfall makes a kind of splash
19.1. The TidBITS News app
19.2. The TidBITS News app’s initial view controller and view hierarchy
19.3. A Latin flashcard app
19.4. The Latin flashcard app’s initial view controller and view hierarchy
19.5. The Latin flashcard app, in drill mode
19.6. The Latin flashcard app’s drill mode view controller and view hierarchy
19.7. Two views that are equal partners
19.8. A familiar navigation interface
19.9. A segmented control in the center of a navigation bar
19.10. A highly dynamic navigation bar
20.1. The Zotz! settings view
20.2. The Zotz! settings view, designed in the nib
20.3. A scroll view coordinated with a page control
20.4. A scrollable map with a draggable flag
21.1. Four table view variations
21.2. A grouped table view as an interface for choosing options
21.3. A cell with a custom gradient background
21.4. A table view with a cell prototype in a storyboard
21.5. A table view cell instantiated from a storyboard
21.6. Designing a static table in the storyboard editor
21.7. Filtering a table with a search bar
21.8. A simple phone directory app
21.9. Phone directory app in editing mode
21.10. A table view cell with a menu
22.1. Two popovers
22.2. A very silly popover
22.3. An automatically created search results popover
23.1. Text whose size increases word by word
23.2. The same text wrapped and centered
23.3. Two-column text in small caps
23.4. The user has tapped on California
24.1. A Help screen that’s a web view
24.2. A web view with dynamically formed content
25.1. A large activity indicator
25.2. A progress view
25.3. A custom progress view
25.4. A picker view
25.5. A search bar with a search results button
25.6. A horrible search bar
25.7. A switch in iOS 5
25.8. A switch in iOS 4
25.9. A stepper
25.10. Repositioning a slider’s images and track
25.11. Replacing a slider’s thumb
25.12. Replacing a slider’s track
25.13. A segmented control
25.14. A button with highlighted glow
25.15. A button with a stretched background image
25.16. A custom control
25.17. A colorful navigation bar
25.18. A navigation bar
25.19. Automatically generated More list
25.20. Customized More list
26.1. An alert view (UIAlertView)
26.2. An action sheet on the iPhone
26.3. An action sheet presented as a popover
26.4. An action sheet presented inside a popover
26.5. A presented view functioning as a modal dialog
26.6. An alert posted by the system when a local notification fires
27.1. The software remote controls in the app switcher
27.2. The software remote controls on the locked screen
27.3. A custom title under the remote controls
28.1. A movie player with controls
28.2. A movie player without controls
28.3. A movie player when the movie is a sound file
28.4. A movie player in fullscreen mode, with controls
28.5. A fullscreen movie player when the movie is a sound file
28.6. The black square’s position is synchronized to the movie
30.1. The user might see this
31.1. A contact created programmatically
32.1. The user specifies a span
34.1. A map view showing a happy place
34.2. A simple annotation
34.3. A custom annotation image
34.4. An overlay view
34.5. A nicer overlay view
36.1. The iTunes file sharing interface
36.2. The document Options action sheet and Open In action sheet
36.3. The People Groups interface
36.4. Defining a custom UTI
39.1. The shake-to-edit undo/redo interface
39.2. The shared menu as an undo/redo interface

List of Examples

1.1. The C flow control constructs
1.2. A switch statement
1.3. Declaring, calling, and defining a function
3.1. Using a pointer to a callback function
3.2. Using a block instead of a callback function
4.1. Conventional schema for defining a class
5.1. The basic pattern for instantiation from scratch
5.2. Polymorphism in action
5.3. Conventional schema for an initializer
10.1. Declaring a method privately
10.2. Enumerating an NSIndexSet before iOS 4.0
10.3. Building an array by enumerating another array
10.4. Parsing a file with Foundation classes
12.1. Imaginary scenario of ARC’s conservative memory management
12.2. How non-ARC code ensures a collection element’s persistence
12.3. Imaginary scenario of how ARC ensures a collection element’s persistence
12.4. A simple retaining setter
12.5. A simple initializer that retains an ivar
12.6. A simple initializer that copies an ivar
12.7. A simple initializer that retains an ivar under ARC
12.8. A simple initializer that copies an ivar under ARC
12.9. The weak–strong dance prevents a copied block from retaining self
12.10. A private property
12.11. Overriding synthesized accessors
15.1. Utility for flipping an image drawing
21.1. The world’s simplest table
21.2. The template code for the third big question
29.1. A presented view on the iPhone, a popover on the iPad