You are previewing The iPhone™ Developer’s Cookbook: Building Applications with the iPhone SDK.
O'Reilly logo
The iPhone™ Developer’s Cookbook: Building Applications with the iPhone SDK

Book Description

“This book would be a bargain at ten times its price! If you are writing iPhone software, it will save you weeks of development time. Erica has included dozens of crisp and clear examples illustrating essential iPhone development techniques and many others that show special effects going way beyond Apple’s official documentation.”

—Tim Burks, iPhone Software Developer, TootSweet Software

“Erica Sadun’s technical expertise lives up to the Addison-Wesley name. The iPhone Developer’s Cookbook is a comprehensive walkthrough of iPhone development that will help anyone out, from beginners to more experienced developers. Code samples and screenshots help punctuate the numerous tips and tricks in this book.”

—Jacqui Cheng, Associate Editor, Ars Technica

“We make our living writing this stuff and yet I am humbled by Erica’s command of her subject matter and the way she presents the material: pleasantly informal, then very appropriately detailed technically. This is a going to be the Petzold book for iPhone developers.”

—Daniel Pasco, Lead Developer and CEO, Black Pixel Luminance

The iPhone Developer’s Cookbook: Building Applications with the iPhone SDK should be the first resource for the beginning iPhone programmer, and is the best supplemental material to Apple’s own documentation.”

—Alex C. Schaefer, Lead Programmer, ApolloIM, iPhone Application Development Specialist, MeLLmo, Inc

“Erica’s book is a truly great resource for Cocoa Touch developers. This book goes far beyond the documentation on Apple’s Web site, and she includes methods that give the developer a deeper understanding of the iPhone OS, by letting them glimpse at what’s going on behind the scenes on this incredible mobile platform.”

—John Zorko, Sr. Software Engineer, Mobile Devices

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:

  • Utilize views and tables

  • Organize interface elements

  • Alert and respond to users

  • Access the Address Book (people), Core Location (places), and Sensors (things)

  • Connect to the Internet and Web services

  • Display media content

  • Create secure Keychain entries

  • And much more

  • 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.

    Table of Contents

    1. Copyright
      1. Dedication
    2. Praise for The iPhone Developer’s Cookbook
    3. Preface
      1. Who This Book Is For
      2. How This Book Is Structured
      3. Prerequisites
      4. Contacting the Author
    4. Acknowledgments
    5. About the Author
    6. 1. Introducing the iPhone SDK
      1. Apple’s iPhone SDK
      2. Assembling iPhone Projects
      3. iPhone Application Components
        1. Application Folder Hierarchy
        2. The Executable
        3. The Info.plist File
        4. The Icon and Default Images
        5. XIB (NIB) files
        6. Files Not Found in the Application Bundle
        7. Sandboxes
      4. Platform Limitations
        1. Storage Limits
        2. Data Access Limits
        3. Memory Limits
        4. Interaction Limits
        5. Energy Limits
        6. Application Limits
        7. User Behavior Limits
      5. SDK Limitations
      6. Programming Paradigms
        1. Object-Oriented Programming
        2. Model-View-Controller
          1. View Classes
          2. Controller
            1. Delegation
            2. Target-Action
          3. Notifications
          4. Model
            1. Data Sources
            2. The UIApplication Object
            3. Uncovering Data Source and Delegate Methods
      7. Building an iPhone Application Skeleton
      8. The Hello World Application
        1. The Classes
        2. The Code
        3. A Note About Sample Code and Memory Management
      9. Building Hello World
        1. Create an iPhone Project
        2. Running the Skeleton
        3. Customize the iPhone Project
        4. Editing Identification Information
        5. Using the Debugger
          1. Set a Breakpoint
          2. Run the Program
          3. Open the Debugger Windows
          4. Inspect
          5. Consider Other Breakpoint Options
      10. Apple’s iPhone Developer Program
        1. Development Phones
        2. Application Identifiers
      11. From Xcode to Your iPhone: The Organizer Interface
        1. Projects and Sources List
        2. Devices List
        3. Summary Tab
        4. Console Tab
        5. Crash Logs Tab
        6. Screenshot Tab
        7. About Tethering
        8. Testing Applications on Your iPhone
        9. Compiling for Distribution
      12. Using Undocumented API Calls
      13. Ad Hoc Distribution
      14. Summary
    7. 2. Views
      1. UIView and UIWindow
        1. Hierarchy
        2. Geometry and Traits
          1. CGRect
          2. CGPoint and CGSize
          3. Defining Locations
          4. Transforms
          5. Other View Traits
          6. View Layout
        3. Gestures
      2. Recipe: Adding Stepwise Subviews
        1. Reorienting
      3. Recipe: Dragging Views
        1. UITouch
        2. Adding Persistence
          1. Storing State
          2. Recovering State
          3. Startup Image
      4. Recipe: Clipped Views
        1. Balancing Touches with Clipping
        2. Accessing Pixel-by-Pixel Values
      5. Recipe: Detecting Multitouch
      6. UIView Animations
        1. Building UIView Animation Blocks
      7. Recipe: Fading a View In and Out
      8. Recipe: Swapping Views
      9. Recipe: Flipping Views
      10. Recipe: Applying CATransitions to Layers
        1. Undocumented Animation Types
        2. General Core Animation Calls
      11. Recipe: Swiping Views
      12. Recipe: Transforming Views
        1. Centering Landscape Views
      13. Summary
    8. 3. View Controllers
      1. View Management
        1. Core Classes
        2. Specialized Classes
        3. Creating a UIViewController
          1. init
          2. shouldAutorotateToInterfaceOrientation:
          3. viewDidAppear: and viewDidDisappear:
          4. didRotateFromInterfaceOrientation:
      2. Working with Interface Builder to Build Views for UIViewControllers
        1. Temperature Conversion Example
        2. Loading XIB Files Directly
      3. Navigation Controllers
        1. Setting Up a Navigation Controller
        2. Pushing and Popping View Controllers
          1. Modal Presentation
        3. The Navigation Item Class
      4. Recipe: Building a Simple Two-Item Menu
      5. Recipe: Adding a Segmented Control
      6. Recipe: Adding a UIToolbar to a Navigation Bar
      7. Recipe: Navigating Between View Controllers
        1. Popping Back to the Root
        2. Loading a View Controller Array
      8. Tab Bars
      9. Summary
    9. 4. Alerting Users
      1. Talking Directly to Your User Through Alerts
        1. Logging Your Results
          1. Redirecting stderr
          2. Building a Custom Log Function
        2. Building Alerts
        3. Displaying the Alert
      2. Recipe: Creating Multiline Button Displays
      3. Recipe: Autotimed No-Button Alerts
      4. Recipe: Soliciting Text Input from the User
      5. Recipe: Presenting Simple Menus
      6. “Please Wait”: Showing Progress to Your User
      7. Recipe: Invoking the Basic Undocumented UIProgressHUD
      8. Recipe: Using UIActivityIndicatorView
      9. Recipe: Building a UIProgressView
      10. Recipe: Adding Custom, Tappable Overlays
      11. Recipe: Building a Scroll-Down Alert
      12. Recipe: Adding Status Bar Images
      13. Adding Application Badges
      14. Recipe: Simple Audio Alerts
        1. Vibration
      15. Summary
    10. 5. Basic Tables
      1. Introducing UITableView and UITableViewController
        1. Creating the Table
          1. Laying Out the View
          2. Assigning a Data Source
          3. Assigning a Delegate
        2. What the UITableViewController Does
      2. Recipe: Creating a Simple List Table
        1. Data Source Functions
        2. Reusing Cells
        3. Font Table Sample
      3. Recipe: Creating a Table-Based Selection Sheet
      4. Recipe: Loading Images into Table Cells
      5. Recipe: Setting a Cell’s Text Traits
      6. Removing Cell Selections
      7. Recipe: Creating Complex Cells
      8. Recipe: Creating Checked Selections
      9. Recipe: Deleting Cells
        1. Creating and Displaying Remove Controls
        2. Dismissing Remove Controls
        3. Handling Delete Requests
        4. Swiping Cells
        5. Adding Cells
      10. Recipe: Reordering Cells
      11. Recipe: Working with Disclosures
      12. Summary
    11. 6. Advanced Tables
      1. Recipe: Grouping Table Selections
        1. Building a Section-Based Data Source
        2. Adding Section Headers
      2. Recipe: Building a Section Table with an Index
      3. Recipe: Custom Cell Backgrounds
        1. Customizing the Table View
      4. Recipe: Creating Alternate Blue and White Cells
      5. Recipe: Framing Tables
      6. Recipe: Adding Coupled Cell Controls
      7. Recipe: Building a Multiwheel Table
        1. Creating the UIPickerView
      8. Recipe: Using the UIDatePicker
        1. Creating the Date Picker
      9. Recipe: Creating Fully Customized Group Tables
        1. Creating Grouped Preferences Tables
      10. Summary
    12. 7. Media
      1. Recipe: Browsing the Documents Folder by File Type
        1. Locating Documents
      2. Loading and Viewing Images
      3. Recipe: Displaying Small Images
      4. Recipe: Using a UIWebView to Display Images
        1. Displaying Web Pages with UIWebView
      5. Recipe: Browsing Your Image Library
      6. Recipe: Selecting and Customizing Images from the Camera Roll
      7. Recipe: Snapping Pictures with the iPhone Camera
      8. Working with iPhone Audio
      9. Recipe: Playing Audio with Celestial
      10. Recipe: Using the Media Player for Audio and Video Playback
      11. Recipe: Recording Audio
      12. Reading in Text Data
        1. Displaying Property Lists
      13. Recovering Media from Backup Files
      14. Summary
    13. 8. Controls
      1. Recipe: Building Simple Buttons
        1. The UIButton class
        2. Building Custom Buttons
        3. Glass Buttons
      2. Recipe: Adding Animated Elements to Buttons
      3. Recipe: Animating Button Responses
      4. Recipe: Customizing Switches
        1. Customizing UIAlertView Buttons
      5. Recipe: Adding Custom Slider Thumbs
        1. Adding Text to the Slider
      6. Recipe: Dismissing a UITextField Keyboard
      7. Recipe: Dismissing UITextView Keyboards
      8. Recipe: Adding an Undo Button to Text Views
      9. Recipe: Creating a Text View–Based HTML Editor
      10. Recipe: Building an Interactive Search Bar
      11. Recipe: Adding Callout Views
      12. Adding a Page Indicator Control
      13. Recipe: Customizing Toolbars
        1. Toolbar Tips
      14. Summary
    14. 9. People, Places, and Things
      1. Address Book Frameworks
        1. Address Book UI
        2. Address Book
          1. Handling People Picker Calls
          2. Querying the Address Book and Its Records
      2. Recipe: Accessing Address Book Image Data
      3. Recipe: Displaying Address Book Information
      4. Recipe: Browsing the Address Book
        1. Browsing for (Only) E-Mail Addresses
        2. Adding New Contacts
      5. Core Location
        1. How Core Location Works
          1. GPS Positioning
          2. SkyHook WiFi Positioning
          3. Google Maps Cell Tower Positioning
          4. SkyHook Internet Provider Positioning
          5. Hybridizing the Approaches
      6. Recipe: Core Location in a Nutshell
      7. Recipe: Reverse Geocoding to an Address
      8. Recipe: Accessing Maps Using Core Location Data
      9. Recipe: Accessing Core Device Information
      10. Recipe: Enabling and Disabling the Proximity Sensor
      11. Recipe: Using Acceleration to Locate “Up”
      12. Recipe: Using Acceleration to Move Onscreen Objects
      13. Summary
    15. 10. Connecting to Services
      1. Recipe: Adding Custom Settings Bundles
        1. Declaring Application Settings
      2. Recipe: Subscribing Applications to Custom URL Schemes
      3. Recipe: Checking Your Network Status
        1. Testing the Network Status
        2. Recovering a Local IP Address
        3. Querying Site IP Addresses
        4. Checking Site Availability
      4. Recipe: Interacting with iPhone Databases
      5. Recipe: Converting XML into Trees
      6. Recipe: Storing and Retrieving Keychain Items
        1. Storing Multiple Keychain Values
        2. Keychain Persistence
      7. Sending and Receiving Files
      8. Recipe: Building a Simple Web-Based Server
      9. Push Notifications
      10. Summary
    16. 11. One More Thing: Programming Cover Flow
      1. The UICoverFlowLayer Class
      2. Building a Cover Flow View
      3. Building a Cover Flow View Controller
        1. Cover Flow Data Source Methods
        2. Cover Flow Delegate Methods
      4. Summary