iPhone™ SDK Programming: A Beginner’s Guide James A. Brannan New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2010 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. ISBN: 978-0-07-162650-7 MHID: 0-07-162650-6 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-162649-1, MHID: 0-07-162649-2. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in cor- porate training programs. To contact a representative please e-mail us at [email protected]. Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WAR- RANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUD- ING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. For Timothy Hill and Doctor Ronald Holt. This book is not some lofty political or anthropological treatise, but it was fun writing. About the Author By day, James A. Brannan works as a Java SOA engineer. By night, he writes computer books. This is his second book, and he has a third on the way. By necessity, he programs using Java during the day, but likes Objective-C much more. He has two kids, a wife, two Macs, and a bicycle, but no dog. He lives in Gaithersburg, Maryland. About the Technical Editor Billy Meyers has been programming for about 15 years, and his most recent work of note is his task manager program for the iPhone called To Do’s by AustinBull Software, which is one of the most popular free task managers available in Apple’s App Store. He began writing code in junior high and high school, and is now working on completing a second degree in Computer Science at Texas A&M University in Commerce, Texas. Contents ACKNOWLEDGMENTS ............................................................. xiii INTRODUCTION ..................................................................... xv 1 The iPhone Software Development Kit (SDK) ............................ 1 The App Store ...................................................................... 2 The Software Development Kit (SDK) .............................................. 4 Paid Membership ............................................................. 5 Objective-C, Foundation Framework, Cocoa Touch, and UIKit ...................... 5 Cocoa Touch ................................................................. 5 Foundation Framework ....................................................... 6 The iPhone Frameworks ...................................................... 6 iPhone Limitations .................................................................. 7 Memory and Processor Speed ................................................. 7 Small Screen ................................................................. 8 Security ...................................................................... 8 Short-Lived Applications ..................................................... 8 Manual Memory Management ................................................ 9 Relevant Documentation ............................................................ 9 Try This: Getting a Quick Start on iPhone Development ............................. 9 Summary ........................................................................... 23 v vi iPhone SDK Programming: A Beginner’s Guide 2 A C Refresher ............................................................. 25 C Command-Line Programs ........................................................ 26 Try This: Creating a Simple C Program Using Xcode ............................... 27 C Comments ....................................................................... 28 Understanding Headers, Import, and Include ........................................ 28 Try This: Creating a Header File .................................................... 28 Preprocessor Statements ............................................................ 30 Try This: Using Preprocessor Statement ............................................. 30 Data Types and Operators ........................................................... 31 Control, Functions, and Conditional Statements ..................................... 32 Arrays and Structures ............................................................... 33 Functions ........................................................................... 34 The printf Method .................................................................. 35 Pointers ............................................................................ 35 Try This: Using Pointers ............................................................ 36 Dereferencing a Pointer ............................................................. 37 Pointers and Arrays ................................................................. 37 Try This: Using an Array with Pointers .............................................. 38 Summary ........................................................................... 39 3 Just Enough Objective-C—Part One ..................................... 41 Objective-C Classes and Objects .................................................... 42 Class Interface and Implementation ................................................. 42 Try This: Generating an Objective-C Class’s Interface and Implementation .......... 43 The @interface and @implementation Compiler Directives ......................... 43 Method Declaration and Definition ................................................. 45 Try This: Adding sayHello to the Simple Class ...................................... 45 Interface Anatomy .................................................................. 46 Implementation Anatomy ........................................................... 47 Public, Private, and Protected Instance Variables .................................... 47 Understanding Simple Messaging ................................................... 49 Using self in a Message ............................................................. 50 Nested Arguments .................................................................. 50 Class and Instance Methods ......................................................... 50 Try This: Adding sayGoodBye as a Class Method to Simple ......................... 51 The alloc and init Methods .......................................................... 53 Managing Memory Using Retain and Release ....................................... 53 Try This: Using Manual Memory Management ...................................... 56 Instance Variables and Memory ..................................................... 57 Managing Memory Using Autorelease .............................................. 59 Summary ........................................................................... 60 Contents vii 4 Just Enough Objective-C—Part Two ..................................... 61 Properties .......................................................................... 62 Retain ........................................................................ 64 Assign ........................................................................ 64 Copy ......................................................................... 65 Releasing Properties .......................................................... 66 Multiple-Argument Messages ....................................................... 66 Try This: Creating a Simple Multiple-Argument Message ........................... 67 Understanding the id Variable Type, Dynamic Typing, and Dynamic Binding ........ 69 The id Type ................................................................... 69 Dynamic Binding and Dynamic Typing ....................................... 69 Understanding Inheritance .......................................................... 70 Overriding Methods .......................................................... 70 Overloading Methods ......................................................... 71 Using Categories ................................................................... 71 Using Protocols ..................................................................... 72 Handling Exceptions ................................................................ 74 Summary ........................................................................... 75 5 Deploying to an iPhone, Debugging, and Testing .......................... 77 Installing Applications on an iPhone ................................................ 79 Membership .................................................................. 79 Certificates, Devices, Application IDs, and Provisioning ....................... 80 Try This: Deploying an Application to iPhone ....................................... 83 Debugging .......................................................................... 88 Using the Debugger ........................................................... 88 Try This: Debugging an Application ................................................ 93 NSZombieEnabled ........................................................... 96 Try This: Enabling Zombies ........................................................ 98 Instruments—Leaks .......................................................... 100 Try This: Find a Memory Leak ..................................................... 101 Find a Memory Leak on iPhone Simulator .................................... 101 Deploying and Distributing Your Application ........................................ 104 Deploying Your Application ................................................... 104 Distributing Your App ......................................................... 105 Summary ........................................................................... 107 6 UIApplication and UIApplicationDelegate ................................ 109 Try This: Adding a UIView and UIViewController to a UIApplicationDelegate ...... 110 Connecting UIWindow, UIApplication, and UIApplicationDelegate ................. 116 Try This: Exploring MainWindow.xib ............................................... 117 viii iPhone SDK Programming: A Beginner’s Guide UIApplication and UIApplicationDelegate .......................................... 118 The main.m file ............................................................... 119 Handling Application Life Cycle Events ...................................... 119 Application Interruptions ..................................................... 122 Try This: Handling Application Interruptions ........................................ 122 Summary ........................................................................... 123 7 UIView and UIViewController ............................................ 125 The UIView Class .................................................................. 126 The UIViewController Class ........................................................ 127 View-based Application Template ................................................... 128 IBOutlet and IBAction .............................................................. 128 Try This: Using a View-based Application Template ................................. 129 Try This: Using a Window-based Application Template ............................. 131 UIViewController and Application Life Cycle Events ............................... 135 Try This: Exploring Several Life Cycle Methods .................................... 136 Summary ........................................................................... 138 8 UITabBar and UITabBarController ...................................... 139 UITabBar, UITabBarController, UITabBarItem, and UITabBarControllerDelegate .... 140 Try This: Using the Tab Bar Application Template .................................. 143 Try This: Adding a Tab Bar Item to a Tab Bar Application ........................... 145 Try This: Creating a Tab Bar Application from Scratch .............................. 147 Try This: Allowing Users to Customize a Tab Bar ................................... 150 Summary ........................................................................... 155 9 UINavigationBar and UINavigationController ........................... 157 UINavigationBar, UINavigationController, and UINavigationItem ................... 160 Try This: Building a Three-View Application Using a Navigation Bar ............... 161 Adding Another View ......................................................... 170 Try This: Duplicating the Utility Application ........................................ 174 More on the UINavigationController ................................................ 178 Pushing and Popping ......................................................... 178 Configuring the Navigation Bar ............................................... 178 Try This: Using a Navigation Controller in a Tab .................................... 179 Summary ........................................................................... 184 10 Tables Using UITableView and UITableViewController .................. 185 UITableView ....................................................................... 187 UITableViewDelegate and UITableViewDataSource ................................. 189 UITableViewDelegate ........................................................ 189 UITableViewDataSource ...................................................... 190 Try This: Adopting the UITableViewDelegate and UITableViewDataSource ......... 191 UITableViewController ............................................................. 198 Contents ix Try This: Using a UITableViewController ........................................... 198 Grouping and Indexing ............................................................. 201 Grouped Table Style .......................................................... 201 Try This: Grouping ................................................................. 202 Indexing ...................................................................... 205 Try This: Indexing .................................................................. 206 Images in Tables .................................................................... 208 Try This: Adding an Image .......................................................... 208 Selecting Rows ..................................................................... 210 Try This: Row Selection ............................................................. 211 Changing Row Height .............................................................. 212 Try This: Changing Row Height ..................................................... 212 Accessorizing Table Cells ........................................................... 213 Try This: Accessorizing a Table Cell ................................................. 213 Customizing a Table Cell ........................................................... 215 Try This: Customizing a Cell Using Interface Builder ................................ 215 Using Tables with Navigation Bars and Tabs ........................................ 220 Try This: Using a Table in a Navigation Controller in a Tab .......................... 220 Editing Table Cells ................................................................. 227 Getting to Edit Mode ......................................................... 227 Edit Mode Methods ........................................................... 228 Try This: Editing Rows .............................................................. 230 Summary ........................................................................... 239 11 Activity, Progress and Alerting Users ..................................... 241 Showing Activity—the UIActivityIndicatorView .................................... 242 Try This: Using a UIActivityIndicatorView .......................................... 245 Showing Progress—the UIProgressView ............................................ 249 Try This: Using a UIProgressView .................................................. 249 Alerting Users ...................................................................... 256 UIAlertView and UIAlertViewDelegate ....................................... 257 Try This: Creating a Simple UIAlertView ........................................... 258 Try This: Using an Alert with Multiple Buttons ...................................... 260 UIActionSheet and UIActionSheetDelegate ................................... 262 Try This: Using a UIActionSheet .................................................... 262 Application Badges ................................................................. 265 Try This: Adding an Application Badge ............................................. 265 Summary ........................................................................... 267 12 Controls—Part One ....................................................... 269 Buttons ............................................................................. 271 UIButton with a Background Image and Image ................................ 271 Try This: Using a Custom Button Background Image and Image ..................... 272 Button Types ....................................................................... 277
Description: