SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
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.
Want to get started building applications for Apple’s iPhone and iPod touch? Already building iPhone applications and want to get better at it? This is the only book that brings together all the expert guidance–and the code–you’ll need!
Completely revised and expanded to cover the iPhone 3.0 SDK, The iPhone Developer’s Cookbook is the essential resource for developers building apps for the iPhone and iPod touch. Taking you further than before, this new edition starts out with an introduction to Objective-C 2.0 for developers who might be new to the platform. You’ll learn about Xcode and Interface Builder and learn how to set up and configure your iPhone Developer account. Additional highlights of this new edition include:
The unique format of The iPhone Developer’s Cookbook presents the code you need to create feature-rich applications that leverage the latest features of the iPhone 3.0 SDK. Over 30,000 iPhone developers turned to the first edition of The iPhone Developer’s Cookbook. So should you!
Preface xix
1 Introducing the iPhone SDK 1
iPhone Developer Programs 1
Getting Started 3
Understanding Model Differences 7
Platform Limitations 9
SDK Limitations 12
Using the Developer Portal 13
Assembling iPhone Projects 17
iPhone Application Components 22
Programming Paradigms 28
Summary 35
2 Building Your First Project 37
Creating New Projects 37
Building Hello World the Template Way 39
Using the Simulator 46
The Minimalist Hello World 48
Using the Debugger 53
Memory Management 58
Recipe: Using Instruments to Detect Leaks 59
Recipe: Using Instruments to Monitor Cached Object Allocations 62
Using the Clang Static Analyzer 64
Building for the iPhone 65
From Xcode to Your iPhone: The Organizer Interface 69
Using Compiler Directives 73
Building for Distribution 78
Clean Builds 80
Building for Ad Hoc Distribution 83
Customizing Xcode Identities 85
Creating Custom Xcode Templates 86
One More Thing: Viewing Side-by-Side Code 88
Summary 89
3 Objective-C Boot Camp 91
The Objective-C Programming Language 91
Classes and Objects 92
Methods, Messages, and Selectors 95
Class Hierarchy 102
Logging Information 103
Properties 105
Simple Memory Management 110
Crafting Singletons 119
Categories (Extending Classes) 120
Protocols 122
Foundation Classes 125
One More Thing: Message Forwarding 138
Summary 141
4 Designing Interfaces 143
UIView and UIWindow 143
UIViewControllers 147
View Design Geometry 151
Building Interfaces 155
Walk-Through: Building a Temperature Converter with IB 156
Walk-Through: Building a Converter Interface by Hand 166
Walk-Through: Creating a Hybrid Converter 169
Walk-Through: Loading .xib Files Directly from Code 173
Designing for Rotation 174
Enabling Reorientation 175
Autosizing 176
Moving Views 179
Swapping Views 183
One More Thing: A Half Dozen Great Interface Builder Tips 184
Summary 186
5 Working with View Controllers 187
Developing with Navigation Controllers 187
Utility Function 190
Recipe: Building a Simple Two-Item Menu 192
Recipe: Adding a Segmented Control 193
Recipe: Navigating Between View Controllers 195
Recipe: Using Creative Popping Options 197
Recipe: Presenting a Custom Modal Information View 199
Recipe: Tab Bars 201
Recipe: Remembering Tab State 204
One More Thing: Interface Builder and Tab Bar Controllers 207
Summary 208
6 Assembling Views and Animations 211
View Hierarchies 211
Recipe: Recovering a View Hierarchy Tree 213
Recipe: Querying Subviews 214
Managing Subviews 215
Recipe: Tagging and Retrieving Views 217
Recipe: Naming Views 219
View Geometry 222
Recipe: Working with View Frames 224
Recipe: Randomly Moving a Bounded View 231
Recipe: Transforming Views 232
Display and Interaction Traits 235
UIView Animations 236
Recipe: Fading a View In and Out 237
Recipe: Swapping Views 239
Recipe: Flipping Views 240
Recipe: Using Core Animation Transitions 242
Recipe: General Core Animation Calls 244
Curl Transitions 246
Recipe: Bouncing Views as They Appear 248
Recipe: Image View Animations 250
One More Thing: Adding Reflections to Views 251
Summary 255
7 Working with Images 257
Recipe: Finding and Loading Images 257
Recipe: Accessing Photos from the iPhone Photo Album 262
Recipe: Selecting and Customizing Images from the Camera Roll 265
Recipe: Snapping Photos and Writing Them to the Photo Album 268
Recipe: Saving Pictures to the Documents Folder 270
Recipe: E-Mailing Pictures 272
Recipe: Capturing Time Lapse Photos 273
Recipe: Using a Custom Camera Overlay 275
Recipe: Displaying Images in a Scrollable View 278
Recipe: Creating a Multiimage Paged Scroll 280
Recipe: Creating New Images from Scratch 281
Recipe: Building Thumbnails from Images 285
Fixing Photo Orientation 288
Taking Screenshots 290
Recipe: Working Directly with Bitmaps 291
One More Thing: Going Grayscale 298
Summary 299
8 Gestures and Touches 301
Touches 301
Recipe: Adding a Simple Direct Manipulation Interface 304
Recipe: Constraining Movement 305
Recipe: Testing Touches 307
Recipe: Testing Against a Bitmap 309
Recipe: Adding Persistence to Direct Manipulation Interfaces 311
Recipe: Persistence Through Archiving 314
Recipe: Adding Undo Support 316
Recipe: Adding Shake-Controlled Undo Support 319
Recipe: Drawing Onscreen 321
Recipe: Calculating Lines 323
Recipe: Detecting Circles 325
Recipe: Detecting Multitouch 327
Recipe: Gesture Distinction 329
One More Thing: Interactive Resize and Rotation 333
Summary 338
9 Building and Using Controls 341
The UIControl Class 341
Buttons 344
Adding Buttons in Interface Builder 345
Building Custom Buttons in Xcode 348
Multiline Button Text 351
Adding Animated Elements to Buttons 351
Recipe: Animating Button Responses 352
Recipe: Working with Switches 354
Recipe: Adding Custom Slider Thumbs 356
Recipe: Creating a Twice-Tappable Segmented Control 362
Recipe: Subclassing UIControl 363
Recipe: Dismissing a UITextField Keyboard 366
Recipe: Dismissing UITextView Keyboards 370
Recipe: Building a Better Text Editor 371
Recipe: Text Entry Filtering 374
Recipe: Adding a Page Indicator Control 376
Recipe: Creating a Customizable Paged Scroller 379
Building a Toolbar 384
One More Thing: Smart Labels 387
Summary 388
10 Alerting Users 391
Talking Directly to Your User Through Alerts 391
Recipe: No-Button Alerts 394
Recipe: Creating Modal Alerts with Run Loops 396
Recipe: Soliciting Text Input from the User 399
Recipe: Using Variadic Arguments with Alert Views 402
Recipe: Presenting Simple Menus 403
Recipe: Displaying Text in Action Sheets 405
“Please Wait”: Showing Progress to Your User 406
Recipe: Building a UIProgressView 407
Recipe: Building Custom Overlays 409
Recipe: Tappable Overlays 411
Recipe: Orientable Scroll-Down Alerts 412
Recipe: Using the Network Activity Indicator 415
Recipe: Badging Applications 416
Recipe: Simple Audio Alerts 417
One More Thing: Showing the Volume Alert 420
Summary 421
11 Creating and Managing Table Views 423
Introducing UITableView and UITableViewController 423
Recipe: Implementing a Very Basic Table 426
Recipe: Changing a Table’s Background Color 430
Recipe: Creating a Table Image Backsplash 432
Recipe: Exploring Cell Types 433
Recipe: Building Custom Cells in Interface Builder 435
Recipe: Alternating Cell Colors 439
Recipe: Building a Custom Cell with Built-In Controls 441
Recipe: Remembering Control State for Custom Cells 443
Recipe: Creating Checked Table Cells 446
Recipe: Removing Selection Highlights from Cells 448
Recipe: Working with Disclosure Accessories 449
Recipe: Deleting Cells 451
Recipe: Reordering Cells 456
Recipe: Adding Undo Support to a Table 457
Recipe: Sorting Tables 462
Recipe: Searching Through a Table 464
Recipe: Working with Sections 467
Recipe: Creating Grouped Tables 473
Recipe: Customizing Headers and Footers 474
Recipe: Creating a Group Table with Many Cell Types and Heights 477
Recipe: Building a Multiwheel Table 480
Recipe: Using a View-Based Picker 484
Recipe: Using the UIDatePicker 487
One More Thing: Formatting Dates 490
Summary 493
12 Making Connections with GameKit and Bonjour 495
Recipe: Creating Basic GameKit Services 495
Recipe: Peeking Behind the Scenes 509
Recipe: Sending Complex Data Through GameKit 510
Recipe: GameKit Voice Chat 512
Recipe: Using Bonjour to Create an iPhone Server 515
Recipe: Creating a Mac Client for an iPhone Bonjour Service 520
Recipe: Working Around Real-World GameKit Limitations 523
Recipe: iPhone to iPhone Gaming Via BonjourHelper 528
Creating an “Online” GameKit Connection 537
One More Thing: Scanning for Services 540
Summary 543
13 Networking 545
Recipe: Checking Your Network Status 545
Recipe: Extending the UIDevice Class for Reachability 547
Recipe: Scanning for Connectivity Changes 549
Recipe: Recovering IP and Host Information 552
Recipe: Checking Site Availability 555
Recipe: Synchronous Downloads 557
Recipe: Asynchronous Downloads 560
Recipe: Handling Authentication Challenges 565
Recipe: Using the Keychain to Store Sensitive Data 566
Recipe: Uploading Via POST 569
Recipe: Uploading Data 572
Recipe: Sharing Keychains Between Applications 575
Recipe: Converting XML into Trees 577
Recipe: Building a Simple Web-Based Server 582
One More Thing: FTPHelper 586
Summary 588
14 Device Capabilities 589
Recipe: Accessing Core Device Information 589
Adding Device Capability Restrictions 590
Recipe: Recovering Additional Device Information 592
Recipe: Monitoring the iPhone Battery State 594
Recipe: Enabling and Disabling the Proximity Sensor 596
Recipe: Using Acceleration to Locate “Up” 597
Recipe: Using Acceleration to Move Onscreen Objects 599
Recipe: Detecting Device Orientation 601
Recipe: Detecting Shakes Using Motion Events 603
Recipe: Detecting Shakes Directly from the Accelerometer 605
One More Thing: Checking for Available Disk Space 608
Summary 609
15 Audio, Video, and MediaKit 611
Recipe: Playing Audio with AVAudioPlayer 611
Recipe: Looping Audio 618
Recipe: Handling Audio Interruptions 621
Recipe: Audio That Ignores Sleep 622
Recipe: Recording Audio 624
Recipe: Recording Audio with Audio Queues 629
Recipe: Playing Video with the Media Player 634
Recipe: Recording Video 636
Recipe: Picking and Editing Video 639
Recipe: Picking Audio with the MPMediaPickerController 641
Creating a Media Query 645
Recipe: Using the MPMusicPlayerController 649
One More Thing: Additional Movie Player Properties 653
Summary 653
16 Push Notifications 655
Introducing Push Notifications 655
Provisioning Push 659
Registering Your Application 662
Recipe: Push Client Skeleton 667
Building Notification Payloads 672
Recipe: Sending Notifications 676
Recipe: Push in Action 681
Feedback Service 685
Designing for Push 686
Summary 687
17 Using Core Location and MapKit 689
How Core Location Works 689
Recipe: Core Location in a Nutshell 691
Recipe: Tracking Speed 695
Recipe: Computing Speed and Distance 696
Recipe: Keeping Track of “North” by Using Heading Values 698
Recipe: Reverse Geocoding 700
Recipe: Viewing a Location 703
Recipe: User Location Annotations 707
Recipe: Creating Map Annotations 709
One More Thing: Geocoding 717
Summary 721
18 Connecting to the Address Book 723
Recipe: Working with the Address Book 723
Recipe: Searching the Address Book 738
Recipe: Accessing Image Data 741
Recipe: Picking People 742
Recipe: Limiting Contact Picker Properties 745
Recipe: Adding New Contacts 747
Recipe: Modifying Existing Contacts 748
Recipe: The ABUnknownPersonViewController 750
One More Thing: Adding Random Contact Art 752
Summary 755
19 A Taste of Core Data 757
Introducing Core Data 757
Recipe: Using Core Data for a Table Data Source 767
Recipe: Search Tables and Core Data 770
Recipe: Integrating Core Data Tables with Live Data Edits 773
Recipe: Implementing Undo-Redo Support with Core Data 775
Summary 778
20 StoreKit: In-App Purchasing 779
Getting Started with StoreKit 779
Creating Test Accounts 781
Creating New In-App Purchase Items 782
Submitting the Application 787
Building a GUI 787
Purchasing Items 789
Validating Receipts 794
Summary 797
21 Accessibility and Other iPhone OS Services 799
Adding VoiceOver Accessibility to Your Apps 799
Recipe: Adding Custom Settings Bundles 806
Recipe: Creating URL-Based Services 814
Summary 819
A Info.plist Keys 821
Index 825