Home > Store

iPhone Developer's Cookbook, The: Building Applications with the iPhone 3.0 SDK, 2nd Edition

Register your product to gain access to bonus material or receive a coupon.

iPhone Developer's Cookbook, The: Building Applications with the iPhone 3.0 SDK, 2nd Edition

eBook

  • Sorry, this book is no longer in print.
  • About Watermarked eBooks
  • 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.

    Watermarked eBook FAQ

Not for Sale

Description

  • Copyright 2010
  • Dimensions: 7" x 9"
  • Edition: 2nd
  • eBook
  • ISBN-10: 0-321-67013-2
  • ISBN-13: 978-0-321-67013-7

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:

  • Using the iPhone SDK’s visual classes and controllers to design and customize interfaces
  • Using gestures, touches, and other sophisticated iPhone interface capabilities
  • Making the most of tables, views, view controllers, and animations
  • Alerting users with progress bars, audio pings, status bar updates, and other indicators
  • Using new Push Notifications to send alerts, whether your app is running or not
  • Playing audio and video with the MediaKit
  • Working with the Address Book, Core Location, and Sensors
  • Connecting to the Internet, Web services, and networks
  • Embedding flexible maps with MapKit and Google Mobile Maps
  • Building multiplayer games with GameKit
  • Using Core Data to build data-driven applications
  • Selling add-on content and services with In-App Purchasing using StoreKit
  • Building accessible apps with Accessibility Plus

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!

Sample Content

Table of Contents

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

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020