You are previewing Beginning iPhone 3 Development: Exploring the iPhone SDK.
O'Reilly logo
Beginning iPhone 3 Development: Exploring the iPhone SDK

Book Description

Assuming only a minimal working knowledge of Objective-C and written in a friendly, easy-to-follow style, Beginning iPhone 3 Development: Exploring the iPhone SDK offers a complete soup-to-nuts course in iPhone and iPod touch programming.

Table of Contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Preface to Beginning iPhone 3 Development
  6. Preface to Beginning iPhone 2 Development
  7. 1. Welcome to the Jungle
    1. 1.1. What This Book Is
    2. 1.2. What You Need Before You Can Begin
    3. 1.3. What You Need to Know Before You Begin
    4. 1.4. What's Different About Coding for iPhone?
      1. 1.4.1. Only One Running Application
      2. 1.4.2. Only One Window
      3. 1.4.3. Limited Access
      4. 1.4.4. Limited Response Time
      5. 1.4.5. Limited Screen Size
      6. 1.4.6. Limited System Resources
      7. 1.4.7. No Garbage Collection
      8. 1.4.8. Some New Stuff
      9. 1.4.9. A Different Approach
    5. 1.5. What's in This Book
      1. 1.5.1. Chapter 2
      2. 1.5.2. Chapter 3
      3. 1.5.3. Chapter 4
      4. 1.5.4. Chapter 5
      5. 1.5.5. Chapter 6
      6. 1.5.6. Chapter 7
      7. 1.5.7. Chapter 8
      8. 1.5.8. Chapter 9
      9. 1.5.9. Chapter 10
      10. 1.5.10. Chapter 11
      11. 1.5.11. Chapter 12
      12. 1.5.12. Chapter 13
      13. 1.5.13. Chapter 14
      14. 1.5.14. Chapter 15
      15. 1.5.15. Chapter 16
      16. 1.5.16. Chapter 17
      17. 1.5.17. Chapter 18
    6. 1.6. What's New in This Update?
    7. 1.7. Are You Ready?
  8. 2. Appeasing the Tiki Gods
    1. 2.1. Setting Up Your Project in Xcode
      1. 2.1.1. The Xcode Project Window
    2. 2.2. Introducing Interface Builder
      1. 2.2.1. What's in the Nib File?
      2. 2.2.2. Adding a Label to the View
    3. 2.3. Some iPhone Polish—Finishing Touches
      1. 2.3.1. Ready to Compile and Run
    4. 2.4. Bring It on Home
  9. 3. Handling Basic Interaction
    1. 3.1. The Model-View-Controller Paradigm
    2. 3.2. Creating Our Project
    3. 3.3. Creating the View Controller
      1. 3.3.1. Outlets
      2. 3.3.2. Actions
      3. 3.3.3. Adding Actions and Outlets to the View Controller
        1. 3.3.3.1. Objective-C Properties
        2. 3.3.3.2. Declaring the Action Method
      4. 3.3.4. Adding Actions and Outlets to the Implementation File
    4. 3.4. Using the Application Delegate
    5. 3.5. Editing MainWindow.xib
    6. 3.6. Editing Button_FunViewController.xib
      1. 3.6.1. Creating the View in Interface Builder
      2. 3.6.2. Connecting Everything
        1. 3.6.2.1. Connecting Outlets
        2. 3.6.2.2. Specifying Actions
      3. 3.6.3. Trying It Out
    7. 3.7. Bring It on Home
  10. 4. More User Interface Fun
    1. 4.1. A Screen Full of Controls
    2. 4.2. Active, Static, and Passive Controls
    3. 4.3. Creating the Application
      1. 4.3.1. Importing the Image
      2. 4.3.2. Implementing the Image View and Text Fields
        1. 4.3.2.1. Determining Outlets
        2. 4.3.2.2. Determining Actions
        3. 4.3.2.3. Building the Interface
      3. 4.3.3. Adding the Image View
        1. 4.3.3.1. Resize the Image View
        2. 4.3.3.2. The Mode Attribute
        3. 4.3.3.3. The Alpha Slider
        4. 4.3.3.4. Ignore the Background
        5. 4.3.3.5. The Tag Attribute
        6. 4.3.3.6. The Drawing Checkboxes
        7. 4.3.3.7. The Interaction Checkboxes
      4. 4.3.4. Adding the Text Fields
        1. 4.3.4.1. The Text Field Inspector Settings
        2. 4.3.4.2. Text Input Traits
        3. 4.3.4.3. And the Rest ...
      5. 4.3.5. Set the Attributes for the Second Text Field
      6. 4.3.6. Connecting Outlets
    4. 4.4. Build and Run
      1. 4.4.1. Making the Keyboard Go Away When Done Is Tapped
      2. 4.4.2. Touching the Background to Close the Keyboard
    5. 4.5. Implementing the Slider and Label
      1. 4.5.1. Determining Outlets
      2. 4.5.2. Determining Actions
      3. 4.5.3. Adding Outlets and Actions
      4. 4.5.4. Adding the Slider and Label
      5. 4.5.5. Connecting the Actions and Outlets
    6. 4.6. Implementing the Switches, Button, and Segmented Control
      1. 4.6.1. Determining Outlets
      2. 4.6.2. Determining Actions
        1. 4.6.2.1. Releasing the Outlets
      3. 4.6.3. Adding the Switches, Button, and Segmented Control
        1. 4.6.3.1. Adding Two Labeled Switches
      4. 4.6.4. Connecting the Switch Outlets and Actions
      5. 4.6.5. Adding the Button
        1. 4.6.5.1. Connecting the Buttons Outlets and Actions
    7. 4.7. Implementing the Action Sheet and Alert
      1. 4.7.1. Conforming to the Action Sheet Delegate Method
      2. 4.7.2. Showing the Action Sheet
      3. 4.7.3. The Action Sheet Delegate and Creating an Alert
    8. 4.8. Spiffing Up the Button
      1. 4.8.1. The viewDidLoad Method
      2. 4.8.2. Control States
      3. 4.8.3. Stretchable Images
    9. 4.9. Being a Good Memory Citizen
    10. 4.10. Crossing the Finish Line
  11. 5. Autorotation and Autosizing
    1. 5.1. Handling Rotation Using Autosize Attributes
      1. 5.1.1. Specifying Rotation Support
      2. 5.1.2. Designing an Interface with Autosize Attributes
      3. 5.1.3. Autosize Attributes
      4. 5.1.4. Setting the Buttons' Autosize Attributes
    2. 5.2. Restructuring a View When Rotated
      1. 5.2.1. Declaring and Connecting Outlets
      2. 5.2.2. Moving the Buttons on Rotation
    3. 5.3. Swapping Views
      1. 5.3.1. Determining Outlets
      2. 5.3.2. Determining Actions
      3. 5.3.3. Declaring Actions and Outlets
      4. 5.3.4. Designing the Two Views
      5. 5.3.5. Implementing the Swap and the Action
    4. 5.4. Rotating Out of Here
  12. 6. Multiview Applications
    1. 6.1. The View Switcher Application
    2. 6.2. The Architecture of a Multiview Application
      1. 6.2.1. Anatomy of a Content View
    3. 6.3. Building View Switcher
      1. 6.3.1. Creating Our View Controller and Nib Files
      2. 6.3.2. Modifying the App Delegate
      3. 6.3.3. SwitchViewController.h
      4. 6.3.4. Modifying MainWindow.xib
      5. 6.3.5. Writing SwitchViewController.m
      6. 6.3.6. Implementing the Content Views
    4. 6.4. Animating the Transition
    5. 6.5. Switching Off
  13. 7. Tab Bars and Pickers
    1. 7.1. The Pickers Application
    2. 7.2. Delegates and Datasources
    3. 7.3. Setting Up the Tab Bar Framework
      1. 7.3.1. Creating the Files
      2. 7.3.2. Adding the Root View Controller
    4. 7.4. Implementing the Date Picker
    5. 7.5. Implementing the Single Component Picker
      1. 7.5.1. Declaring Outlets and Actions
      2. 7.5.2. Building the View
      3. 7.5.3. Implementing the Controller as Datasource and Delegate
    6. 7.6. Implementing a Multicomponent Picker
      1. 7.6.1. Declaring Outlets and Actions
      2. 7.6.2. Building the View
      3. 7.6.3. Implementing the Controller
    7. 7.7. Implementing Dependent Components
    8. 7.8. Creating a Simple Game with a Custom Picker
      1. 7.8.1. Writing the Controller Header File
      2. 7.8.2. Building the View
      3. 7.8.3. Adding Image Resources
      4. 7.8.4. Implementing the Controller
      5. 7.8.5. The spin Method
      6. 7.8.6. The viewDidLoad Method
      7. 7.8.7. Final Details
      8. 7.8.8. Linking in the Audio Toolbox Framework
    9. 7.9. Final Spin
  14. 8. Introduction to Table Views
    1. 8.1. Table View Basics
      1. 8.1.1. Grouped and Plain Tables
    2. 8.2. Implementing a Simple Table
      1. 8.2.1. Designing the View
      2. 8.2.2. Writing the Controller
    3. 8.3. Adding an Image
      1. 8.3.1. Table View Cell Styles
    4. 8.4. Additional Configurations
      1. 8.4.1. Setting the Indent Level
      2. 8.4.2. Handling Row Selection
      3. 8.4.3. Changing Font Size and Row Height
      4. 8.4.4. What Else Can the Delegate Do?
    5. 8.5. Customizing Table View Cells
      1. 8.5.1. The Cells Application
      2. 8.5.2. Adding Subviews to the Table View Cell
        1. 8.5.2.1. Modifying the Controller Header File
        2. 8.5.2.2. Implementing the Controller's Code
      3. 8.5.3. Using a Custom Subclass of UITableViewCell
        1. 8.5.3.1. Creating the UITableViewCell Subclass
        2. 8.5.3.2. Designing the Table View Cell in Interface Builder
        3. 8.5.3.3. Using the New Table View Cell
    6. 8.6. Grouped and Indexed Sections
      1. 8.6.1. Building the View
      2. 8.6.2. Importing the Data
      3. 8.6.3. Implementing the Controller
      4. 8.6.4. Adding an Index
    7. 8.7. Implementing a Search Bar
      1. 8.7.1. Rethinking the Design
      2. 8.7.2. A Deep Mutable Copy
      3. 8.7.3. Updating the Controller Header File
      4. 8.7.4. Modifying the View
      5. 8.7.5. Modifying the Controller Implementation
        1. 8.7.5.1. Copying Data from allNames
        2. 8.7.5.2. Implementing the Search
        3. 8.7.5.3. Changes to viewDidLoad
        4. 8.7.5.4. Changes to Datasource Methods
        5. 8.7.5.5. Adding a Table View Delegate Method
        6. 8.7.5.6. Adding Search Bar Delegate Methods
        7. 8.7.5.7. Adding a Magnifying Glass to the Index
          1. 8.7.5.7.1. ADDING THE SPECIAL VALUE TO THE KEYS ARRAY
          2. 8.7.5.7.2. SUPPRESSING THE SECTION HEADER
          3. 8.7.5.7.3. TELLING THE TABLE VIEW WHAT TO DO
    8. 8.8. Putting It All on the Table
  15. 9. Navigation Controllers and Table Views
    1. 9.1. Navigation Controllers
      1. 9.1.1. Stacky Goodness
      2. 9.1.2. A Stack of Controllers
    2. 9.2. Nav, a Hierarchical Application in Six Parts
    3. 9.3. Constructing the Nav Application's Skeleton
      1. 9.3.1. Creating the First Level View Controller
      2. 9.3.2. Setting Up the Navigation Controller
    4. 9.4. Our First Subcontroller: The Disclosure Button View
    5. 9.5. Our Second Subcontroller: The Checklist
    6. 9.6. Our Third Subcontroller: Controls on Table Rows
    7. 9.7. Our Fourth Subcontroller: Moveable Rows
      1. 9.7.1. Editing Mode
      2. 9.7.2. Creating a New Second-Level Controller
    8. 9.8. Our Fifth Subcontroller: Deletable Rows
    9. 9.9. Our Sixth Subcontroller: An Editable Detail Pane
      1. 9.9.1. Creating the Data Model Object
      2. 9.9.2. Creating the Controllers
      3. 9.9.3. Creating the Detail View Controller
    10. 9.10. But There's One More Thing...
    11. 9.11. Breaking the Tape
  16. 10. Application Settings and User Defaults
    1. 10.1. Getting to Know Your Settings Bundle
    2. 10.2. The AppSettings Application
    3. 10.3. Creating the Project
    4. 10.4. Working with the Settings Bundle
      1. 10.4.1. Adding a Settings Bundle to Our Project
      2. 10.4.2. Setting Up the Property List
      3. 10.4.3. Adding a Text Field Setting
      4. 10.4.4. Adding a Secure Text Field Setting
      5. 10.4.5. Adding a Multivalue Field
      6. 10.4.6. Adding a Toggle Switch Setting
      7. 10.4.7. Adding the Slider Setting
      8. 10.4.8. Adding a Child Settings View
    5. 10.5. Reading Settings in Our Application
    6. 10.6. Changing Defaults from Our Application
    7. 10.7. Beam Me Up, Scotty
  17. 11. Basic Data Persistence
    1. 11.1. Your Application's Sandbox
      1. 11.1.1. Getting the Documents Directory
      2. 11.1.2. Getting the tmp Directory
    2. 11.2. File Saving Strategies
      1. 11.2.1. Single-File Persistence
      2. 11.2.2. Multiple-File Persistence
    3. 11.3. Persisting Application Data
      1. 11.3.1. Property List Serialization
    4. 11.4. The Persistence Application
      1. 11.4.1. Creating the Persistence Project
      2. 11.4.2. Designing the Persistence Application View
      3. 11.4.3. Editing the Persistence Classes
      4. 11.4.4. Archiving Model Objects
        1. 11.4.4.1. Conforming to NSCoding
      5. 11.4.5. Implementing NSCopying
        1. 11.4.5.1. Archiving a Data Object
        2. 11.4.5.2. Unarchiving a Data Object
    5. 11.5. The Archiving Application
      1. 11.5.1. Implementing the FourLines Class
      2. 11.5.2. Implementing the PersistenceViewController Class
    6. 11.6. Using iPhone's Embedded SQLite3
      1. 11.6.1.
        1. 11.6.1.1. Creating or Opening the Database
        2. 11.6.1.2. Bind Variables
      2. 11.6.2. Setting Up a Project to Use SQLite3
    7. 11.7. Using Core Data
      1. 11.7.1. Entities and Managed Objects
      2. 11.7.2. Key-Value Coding
      3. 11.7.3. Putting It All in Context
      4. 11.7.4. Creating New Managed Objects
      5. 11.7.5. Retrieving Managed Objects
      6. 11.7.6. Designing the Data Model
      7. 11.7.7. Creating the Persistence View and Controller
      8. 11.7.8. Making Persistence View Controller our Application's Root Controller
    8. 11.8. Persistence Rewarded
  18. 12. Drawing with Quartz and OpenGL
    1. 12.1. Two Views of a Graphical World
    2. 12.2. This Chapter's Drawing Application
    3. 12.3. The Quartz Approach to Drawing
      1. 12.3.1. Quartz 2D's Graphics Contexts
      2. 12.3.2. The Coordinates System
      3. 12.3.3. Specifying Colors
        1. 12.3.3.1. A Bit of Color Theory for Your iPhone's Display
        2. 12.3.3.2. More Than Color Meets the Eye
      4. 12.3.4. Drawing Images in Context
      5. 12.3.5. Drawing Shapes: Polygons, Lines, and Curves
      6. 12.3.6. Quartz 2D Tool Sampler: Patterns, Gradients, and Dash Patterns
    4. 12.4. Building the QuartzFun Application
      1. 12.4.1. Creating a Random Color
      2. 12.4.2. Defining Application Constants
      3. 12.4.3. Implementing the QuartzFunView Skeleton
      4. 12.4.4. Adding Outlets and Actions to the View Controller
      5. 12.4.5. Updating QuartzFunViewController.xib
      6. 12.4.6. Drawing the Line
      7. 12.4.7. Drawing the Rectangle and Ellipse
      8. 12.4.8. Drawing the Image
      9. 12.4.9. Optimizing the QuartzFun Application
    5. 12.5. Some OpenGL ES Basics
      1. 12.5.1. Building the GLFun Application
      2. 12.5.2. Design the Nib, Add the Frameworks, Run the App
    6. 12.6. Drawing a Blank
  19. 13. Taps, Touches, and Gestures
    1. 13.1. Multitouch Terminology
    2. 13.2. The Responder Chain
      1. 13.2.1. Forwarding an Event: Keeping the Responder Chain Alive
    3. 13.3. The Multitouch Architecture
      1. 13.3.1. The Four Gesture Notification Methods
    4. 13.4. The Touch Explorer Application
    5. 13.5. The Swipes Application
    6. 13.6. Implementing Multiple Swipes
    7. 13.7. Detecting Multiple Taps
    8. 13.8. Detecting Pinches
    9. 13.9. Defining Custom Gestures
      1. 13.9.1. The CheckPlease Touch Methods
    10. 13.10. Garçon? Check, Please!
  20. 14. Where Am I? Finding Your Way with Core Location
    1. 14.1. The Location Manager
      1. 14.1.1. Setting the Desired Accuracy
      2. 14.1.2. Setting the Distance Filter
      3. 14.1.3. Starting the Location Manager
      4. 14.1.4. Using the Location Manager Wisely
    2. 14.2. The Location Manager Delegate
      1. 14.2.1. Getting Location Updates
      2. 14.2.2. Getting Latitude and Longitude Using CLLocation
      3. 14.2.3. Error Notifications
    3. 14.3. Trying Out Core Location
      1. 14.3.1. Updating Location Manager
      2. 14.3.2. Determining Distance Traveled
    4. 14.4. Wherever You Go, There You Are
  21. 15. Whee! Accelerometer!
    1. 15.1. Accelerometer Physics
    2. 15.2. Accessing the Accelerometer
      1. 15.2.1. UIAcceleration
      2. 15.2.2. Implementing the accelerometer:didAccelerate: Method
        1. 15.2.2.1. Detecting Shakes
        2. 15.2.2.2. Accelerometer as Directional Controller
    3. 15.3. Shake and Break
      1. 15.3.1. The Code That Breaks
      2. 15.3.2. Load the Simulation Files
      3. 15.3.3. All Better—The Healing Touch
    4. 15.4. The Rolling Marble Program
      1. 15.4.1. Implementing the Ball View Controller
      2. 15.4.2. Writing the Ball View
      3. 15.4.3. Calculating Ball Movement
    5. 15.5. Rolling On
  22. 16. iPhone Camera and Photo Library
    1. 16.1. Using the Image Picker and UIImagePickerController
    2. 16.2. Implementing the Image Picker Controller Delegate
    3. 16.3. Road Testing the Camera and Library
      1. 16.3.1. Designing the Interface
      2. 16.3.2. Implementing the Camera View Controller
    4. 16.4. It's a Snap!
  23. 17. Application Localization
    1. 17.1. Localization Architecture
    2. 17.2. Using String Files
      1. 17.2.1. Creating the Strings File
    3. 17.3. Real-World iPhone: Localizing Your Application
      1. 17.3.1. Looking at the Current Locale
      2. 17.3.2. Trying Out LocalizeMe
      3. 17.3.3. Localizing the Nib
      4. 17.3.4. Looking at the Localized Project Structure
      5. 17.3.5. Localizing an Image
      6. 17.3.6. Localizing the Application Icon
      7. 17.3.7. Generating and Localizing a Strings File
    4. 17.4. Auf Wiedersehen
  24. 18. Where to Next?
    1. 18.1. Getting Unstuck
      1. 18.1.1. Apple's Documentation
      2. 18.1.2. Mailing Lists
      3. 18.1.3. Discussion Forums
      4. 18.1.4. Web Sites
      5. 18.1.5. Blogs
      6. 18.1.6. Dave and Jeff Blogs and Twitter
      7. 18.1.7. More iPhone 3 Development
      8. 18.1.8. And If All Else Fails...
    2. 18.2. Farewell