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

Book Description

Are you a programmer looking for a new challenge? Does the thought of building your very own iPhone app make your heart race and your pulse quicken? If so, then Beginning iPhone Development is just the book for you.

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

The book starts with the basics, walking you through the process of downloading and installing Apple's free iPhone SDK, then stepping you though the creation of your first simple iPhone application. You'll move on from there, mastering all the iPhone interface elements that you've come to know and love, such as buttons, switches, pickers, toolbars, sliders, etc.

You'll master a variety of design patterns, from the simplest single view to complex hierarchical drill-downs. You'll master the art of table-building and learn how to save your data using the iPhone file system. You'll also learn how to save and retrieve your data using SQLite, iPhone's built-in database management system.

You'll learn how to draw using Quartz 2D and OpenGL ES. You'll add MultiTouch Gestural Support (pinches and swipes) to your applications, and work with the Camera, Photo Library, and Accelerometer. You'll master application preferences, learn how to localize your apps into other languages, and so much more.

Apple's iPhone SDK, this book, and your imagination are all you'll need to start building your very own best-selling iPhone applications.

Table of Contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Preface
  6. 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. Missing Cocoa Tools
      8. 1.4.8. Some New Stuff
      9. 1.4.9. A Different Approach
    5. 1.5. What's in This Book
    6. 1.6. Are You Ready?
  7. 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
  8. 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
      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
      3. 3.6.3. Trying It Out
    7. 3.7. Bring It on Home
  9. 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
      3. 4.3.3. Adding the Image View
      4. 4.3.4. Adding the Text Fields
      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 and Segmented Control
      1. 4.6.1. Determining Outlets
      2. 4.6.2. Determining Actions
      3. 4.6.3. Adding the Switches and Segmented Control
      4. 4.6.4. Connecting the Outlets
    7. 4.7. Implementing the Button, Action Sheet, and Alert
      1. 4.7.1. Adding the Outlet and Action to the Controller Header File
      2. 4.7.2. Adding the Button in Interface Builder
      3. 4.7.3. Implementing the Button's Action Method
    8. 4.8. Showing an Action Sheet
      1. 4.8.1. The Action Sheet Delegate and Creating an Alert
    9. 4.9. Spiffing Up the Button
      1. 4.9.1. The viewDidLoad Method
      2. 4.9.2. Control States
      3. 4.9.3. Stretchable Images
    10. 4.10. Crossing the Finish Line
  10. 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
      6. 5.3.6. Linking in the Core Graphics Framework
    4. 5.4. Rotating Out of Here
  11. 6. Multiview Applications
    1. 6.1. The View Switcher Application
    2. 6.2. The Architecture of a Multiview Application
      1. 6.2.1. Multiview Controllers Are View Controllers
      2. 6.2.2. 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. Refactoring
    6. 6.6. Switching Off
  12. 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. Setting Up the Content View Nibs
      3. 7.3.3. 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
  13. 8. Introduction to Table Views
    1. 8.1. Table View Basics
      1. 8.1.1. Grouped and Indexed 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
    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
      3. 8.5.3. Modifying the controller Header File
      4. 8.5.4. Using a Custom Subclass of UITableViewCell
    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
    8. 8.8. Putting It All on the Table
  14. 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 Root 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
  15. 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
  16. 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
      5. 11.4.5. Implementing NSCopying
    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. Setting Up a Project to Use SQLite3
    7. 11.7. Persistence Rewarded
  17. 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
      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
    5. 12.5. Some OpenGL ES Basics
      1. 12.5.1. Building the GLFun Application
    6. 12.6. Drawing a Blank
  18. 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!
  19. 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
  20. 15. Whee!
    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
    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
  21. 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!
  22. 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
  23. 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. And If All Else Fails...
    2. 18.2. Farewell