Register your product to gain access to bonus material or receive a coupon.
This PDF will be accessible from your Account page after purchase and requires PDF reading software, such as Acrobat® Reader®.
The eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
This is the eBook version of the printed book. If the print book includes a CD-ROM, this content is not included within the eBook version.
The iPhone and iPod touch aren’t just attracting millions of new users; their breakthrough development platform enables programmers to build tomorrow’s killer applications. If you’re getting started with iPhone programming, this book brings together tested, ready-to-use code for hundreds of the challenges you’re most likely to encounter. Use this fully documented, easy-to-customize code to get productive fast—and focus your time on the specifics of your application, not boilerplate tasks.
Leading iPhone developer Erica Sadun begins by exploring the iPhone delivery platform and SDK, helping you set up your development environment, and showing how iPhone applications are constructed. Next, she offers single-task recipes for the full spectrum of iPhone/iPod touch programming jobs:
You’ll even discover how to use Cover Flow to create gorgeous visual selection experiences that put scrolling lists to shame!
This book is organized for fast access: related tasks are grouped together, and you can jump directly to the right solution, even if you don’t know which class or framework to use. All code is based on Apple’s publicly released iPhone SDK, not a beta. No matter what iPhone projects come your way, The iPhone Developer’s Cookbook will be your indispensable companion.
Preface xvii
Acknowledgments xxi
About the Author xxii
1 Introducing the iPhone SDK 1
Apple’s iPhone SDK 1
Assembling iPhone Projects 2
iPhone Application Components 4
Application Folder Hierarchy 4
The Executable 4
The Info.plist File 4
The Icon and Default Images 6
XIB (NIB) files 6
Files Not Found in the Application Bundle 7
Sandboxes 7
Platform Limitations 8
Storage Limits 8
Data Access Limits 8
Memory Limits 8
Interaction Limits 9
Energy Limits 9
Application Limits 9
User Behavior Limits 10
SDK Limitations 10
Programming Paradigms 11
Object-Oriented Programming 11
Model-View-Controller 11
Building an iPhone Application Skeleton 18
The Hello World Application 19
The Classes 19
The Code 20
A Note About Sample Code and Memory Management 20
Building Hello World 23
Create an iPhone Project 23
Running the Skeleton 24
Customize the iPhone Project 24
Editing Identification Information 25
Using the Debugger 26
Apple’s iPhone Developer Program 28
Development Phones 28
Application Identifiers 29
From Xcode to Your iPhone: The Organizer Interface 30
Projects and Sources List 30
Devices List 31
Summary Tab 31
Console Tab 31
Crash Logs Tab 31
Screenshot Tab 32
About Tethering 32
Testing Applications on Your iPhone 32
Compiling for Distribution 33
Using Undocumented API Calls 34
Ad Hoc Distribution 35
Summary 36
2 Views 37
UIView and UIWindow 37
Hierarchy 37
Geometry and Traits 39
Gestures 42
Recipe: Adding Stepwise Subviews 42
Reorienting 44
Recipe: Dragging Views 45
UITouch 46
Adding Persistence 48
Recipe: Clipped Views 51
Balancing Touches with Clipping 53
Accessing Pixel-by-Pixel Values 54
Recipe: Detecting Multitouch 56
UIView Animations 59
Building UIView Animation Blocks 59
Recipe: Fading a View In and Out 60
Recipe: Swapping Views 62
Recipe: Flipping Views 64
Recipe: Applying CATransitions to Layers 66
Undocumented Animation Types 67
General Core Animation Calls 68
Recipe: Swiping Views 69
Recipe: Transforming Views 72
Centering Landscape Views 74
Summary 74
3 View Controllers 77
View Management 77
Core Classes 77
Specialized Classes 78
Creating a UIViewController 79
Working with Interface Builder to Build Views for
UIViewControllers 81
Temperature Conversion Example 81
Loading XIB Files Directly 90
Navigation Controllers 91
Setting Up a Navigation Controller 91
Pushing and Popping View Controllers 92
The Navigation Item Class 92
Recipe: Building a Simple Two-Item Menu 93
Recipe: Adding a Segmented Control 95
Recipe: Adding a UIToolbar to a Navigation Bar 97
Recipe: Navigating Between View Controllers 100
Popping Back to the Root 102
Loading a View Controller Array 102
Tab Bars 103
Summary 106
4 Alerting Users 107
Talking Directly to Your User Through Alerts 107
Logging Your Results 108
Building Alerts 109
Displaying the Alert 110
Recipe: Creating Multiline Button Displays 110
Recipe: Autotimed No-Button Alerts 112
Recipe: Soliciting Text Input from the User 113
Recipe: Presenting Simple Menus 115
“Please Wait”: Showing Progress to Your User 117
Recipe: Invoking the Basic Undocumented UIProgressHUD 117
Recipe: Using UIActivityIndicatorView 119
Recipe: Building a UIProgressView 121
Recipe: Adding Custom, Tappable Overlays 123
Recipe: Building a Scroll-Down Alert 127
Recipe: Adding Status Bar Images 131
Adding Application Badges 132
Recipe: Simple Audio Alerts 134
Vibration 136
Summary 136
5 Basic Tables 139
Introducing UITableView and UITableViewController 139
Creating the Table 140
What the UITableViewController Does 141
Recipe: Creating a Simple List Table 142
Data Source Functions 142
Reusing Cells 143
Font Table Sample 143
Recipe: Creating a Table-Based Selection Sheet 145
Recipe: Loading Images into Table Cells 149
Recipe: Setting a Cell’s Text Traits 151
Removing Cell Selections 152
Recipe: Creating Complex Cells 153
Recipe: Creating Checked Selections 155
Recipe: Deleting Cells 157
Creating and Displaying Remove Controls 157
Dismissing Remove Controls 158
Handling Delete Requests 158
Swiping Cells 158
Adding Cells 159
Recipe: Reordering Cells 161
Recipe: Working with Disclosures 162
Summary 164
6 Advanced Tables 165
Recipe: Grouping Table Selections 165
Building a Section-Based Data Source 166
Adding Section Headers 170
Recipe: Building a Section Table with an Index 171
Recipe: Custom Cell Backgrounds 172
Customizing the Table View 176
Recipe: Creating Alternate Blue and White Cells 177
Recipe: Framing Tables 179
Recipe: Adding Coupled Cell Controls 180
Recipe: Building a Multiwheel Table 182
Creating the UIPickerView 183
Recipe: Using the UIDatePicker 186
Creating the Date Picker 186
Recipe: Creating Fully Customized Group Tables 189
Creating Grouped Preferences Tables 189
Summary 195
7 Media 197
Recipe: Browsing the Documents Folder by File Type 197
Locating Documents 198
Loading and Viewing Images 200
Recipe: Displaying Small Images 201
Recipe: Using a UIWebView to Display Images 203
Displaying Web Pages with UIWebView 205
Recipe: Browsing Your Image Library 206
Recipe: Selecting and Customizing Images from the Camera Roll 209
Recipe: Snapping Pictures with the iPhone Camera 212
Working with iPhone Audio 214
Recipe: Playing Audio with Celestial 215
Recipe: Using the Media Player for Audio and Video Playback 217
Recipe: Recording Audio 219
Reading in Text Data 227
Displaying Property Lists 227
Recovering Media from Backup Files 228
Summary 229
8 Controls 231
Recipe: Building Simple Buttons 231
The UIButton class 232
Building Custom Buttons 233
Glass Buttons 236
Recipe: Adding Animated Elements to Buttons 236
Recipe: Animating Button Responses 238
Recipe: Customizing Switches 239
Customizing UIAlertView Buttons 241
Recipe: Adding Custom Slider Thumbs 242
Adding Text to the Slider 246
Recipe: Dismissing a UITextField Keyboard 246
Recipe: Dismissing UITextView Keyboards 248
Recipe: Adding an Undo Button to Text Views 250
Recipe: Creating a Text View—Based HTML Editor 253
Recipe: Building an Interactive Search Bar 255
Recipe: Adding Callout Views 258
Adding a Page Indicator Control 260
Recipe: Customizing Toolbars 263
Toolbar Tips 266
Summary 267
9 People, Places, and Things 269
Address Book Frameworks 269
Address Book UI 269
Address Book 270
Recipe: Accessing Address Book Image Data 271
Recipe: Displaying Address Book Information 273
Recipe: Browsing the Address Book 274
Browsing for (Only) E-Mail Addresses 277
Adding New Contacts 277
Core Location 278
How Core Location Works 278
Recipe: Core Location in a Nutshell 280
Recipe: Reverse Geocoding to an Address 283
Recipe: Accessing Maps Using Core Location Data 286
Recipe: Accessing Core Device Information 288
Recipe: Enabling and Disabling the Proximity Sensor 289
Recipe: Using Acceleration to Locate “Up” 290
Recipe: Using Acceleration to Move Onscreen Objects 292
Summary 295
10 Connecting to Services 297
Recipe: Adding Custom Settings Bundles 297
Declaring Application Settings 297
Recipe: Subscribing Applications to Custom URL Schemes 302
Recipe: Checking Your Network Status 304
Testing the Network Status 304
Recovering a Local IP Address 305
Querying Site IP Addresses 306
Checking Site Availability 307
Recipe: Interacting with iPhone Databases 308
Recipe: Converting XML into Trees 311
Recipe: Storing and Retrieving Keychain Items 313
Storing Multiple Keychain Values 318
Keychain Persistence 319
Sending and Receiving Files 320
Recipe: Building a Simple Web-Based Server 321
Push Notifications 325
Summary 326
11 One More Thing: Programming Cover Flow 327
The UICoverFlowLayer Class 327
Building a Cover Flow View 329
Building a Cover Flow View Controller 331
Cover Flow Data Source Methods 332
Cover Flow Delegate Methods 333
Summary 336
Index 357