www.it-ebooks.info www.it-ebooks.info iPhone App Development THE MISSING MANUAL The book that should have been in the box ®ˇ www.it-ebooks.info Download from Wow! eBook www.it-ebooks.info iPhone App Development Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo Craig Hockenberry www.it-ebooks.info iPhone App Development: The Missing Manual by Craig Hockenberry Copyright © 2010 Craig Hockenberry. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles: my.safaribooksonline.com. For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. May 2010: First Edition. The Missing Manual is a registered trademark of O’Reilly Media, Inc. The Missing Manual logo, and “The book that should have been in the box” are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media is aware of a trademark claim, the designations are capitalized. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained in it. This book uses a durable and flexible lay-flat binding. ISBN: 9780596809775 [M] www.it-ebooks.info v Table of Contents The Missing Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Part One: Getting Started with Cocoa Touch Chapter 1: Building Your First iPhone App . . . . . . . . . . . . . . 9 Getting the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing Xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Getting the iPhone SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 What Lies Ahead for the SDK? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Exploring Your New Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Every Flashlight Needs a Parts List . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Some Assembly Required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Taking It for a Run on Your Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Revision Decision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Chapter 2: The Power of Brackets . . . . . . . . . . . . . . . . . . 29 Objective-C: The Nuts and Bolts for Your iPhone App . . . . . . . . . . . . . . . . . . . . 30 The Land of Square Brackets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 The Object of It All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Telling Your Objects to Do Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Masses of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Classes in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 The Methods Behind the Madness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Categorically Speaking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Implementation: The Brains Behind the Beauty . . . . . . . . . . . . . . . . . . . . . 39 Creating New Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 www.it-ebooks.info vi Managing Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Take a nil Pill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Autorelease with Ease . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Properties and Dots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Methods of Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Initializing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Deallocation Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Loops: For Better or For Worse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Your Exceptional Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Learn by Crashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Selector Projector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Show Your id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Developer Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Learn to Be Lazy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Chapter 3: Cocoa Touch: Putting Objective-C to Work . . . . . . 65 Get in Cocoa Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The Big Three: Models, Views, Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Value Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Let’s Get Primitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Objectified . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Copying in Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Property Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Mutable Versus Immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Make It Mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Protect Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Delegation and Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Targets and Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 User Interface: The Hard Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 User Interface: The Easy Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Singletons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Singletons as Globals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 The Language of Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Chapter 4: Design Tools: Building a Better Flashlight . . . . . . . 103 Plan Before You Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Why Call in a Designer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Design Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 What’s Unique about iPhone Design? . . . . . . . . . . . . . . . . . . . . . . . . . 105 www.it-ebooks.info vii The Design Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Living in Harmony with Your Designer . . . . . . . . . . . . . . . . . . . . . . . . . 111 Feedback: Don’t Take Your Own Word for It . . . . . . . . . . . . . . . . . . . . . . . . 113 The Providers of Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Flashlight 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Bigger, Stronger, Faster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 The Light Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 The Flip Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 The Drawing Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Technical Design: Between Pictures and Code . . . . . . . . . . . . . . . . . . . . . . . 118 Start Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Ready to Code! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Part Two: Development in Depth Chapter 5: Getting Serious about Development . . . . . . . . . . 125 Beyond the Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Pick Your SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Change Project Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Change Target Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Build Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Duplicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Make It Official . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Join the iPhone Developer Program . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Open the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 When It Fails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 You’re Now Mobile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Ready to Roll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Chapter 6: A Flashlight for Pros . . . . . . . . . . . . . . . . . . . . 155 A Guided Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Where to Start? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Open the Guide Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Flashlight_ProAppDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 NIB Files: Something to Look At . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 MainWindow.xib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 MainView.xib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 View Sizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Autoresizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 SettingsView.xib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 AboutView.xib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Refine the Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 LightView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 IFInfoView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 www.it-ebooks.info viii Making Cocoa Touch Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 UIColor+Brightness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 UIView+Brightness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 UIView+Concealed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Fashion Your Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 SOSModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 FlashlightModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Pull Yourself Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 MainViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 SettingsViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 AboutViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Localized Languages: Capisce? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Localizable.strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 AboutView.xib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Wrapping It Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Part Three: The Business End Chapter 7: Finishing Touches . . . . . . . . . . . . . . . . . . . . . . 213 Beta Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Your App Works for You…But . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Ad Hoc & Roll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Clean Up Your Act . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Beautification Committee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Speaking in Tongues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Web Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 The Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 App Store, Here You Come . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Chapter 8: For Sale . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Sign on the Dotted Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 First Order of Business . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Stake Your Claim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Export Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Ratings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Upload—Show It Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Pricing—Pick Your Price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Tweak It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 www.it-ebooks.info [...]... Windows XP Pro: The Missing Manual, Second Edition by David Pogue, Craig Zacker, and Linda Zacker xiv iPhone App Development: The Missing Manual www.it-ebooks.info Windows Vista: The Missing Manual by David Pogue Windows 7: The Missing Manual by David Pogue Word 2007: The Missing Manual by Chris Grover Your Body: The Missing Manual by Matthew MacDonald Your Brain: The Missing Manual by Matthew MacDonald... Aaron Miller iPad: The Missing Manual by J.D Biersdorfer and David Pogue iPhone: The Missing Manual, Second Edition by David Pogue iPhone App Development: The Missing Manual by Craig Hockenberry iPhoto ’08: The Missing Manual by David Pogue iPhoto ’09: The Missing Manual by David Pogue and J.D Biersdorfer iPod: The Missing Manual, Eighth Edition by J.D Biersdorfer and David Pogue The Missing Credits xiii... Pogue’s Digital Photography: The Missing Manual by David Pogue Dreamweaver CS4: The Missing Manual by David Sawyer McFarland Dreamweaver CS5: The Missing Manual by David Sawyer McFarland Excel 2007: The Missing Manual by Matthew MacDonald Excel 2010: The Missing Manual by Matthew MacDonald Facebook: The Missing Manual, Second Edition by E.A Vander Veer FileMaker Pro 10: The Missing Manual by Susan Prosser... JavaScript: The Missing Manual by David Sawyer McFarland Living Green: The Missing Manual by Nancy Conner Mac OS X: The Missing Manual, Leopard Edition by David Pogue Mac OS X Snow Leopard: The Missing Manual by David Pogue Microsoft Project 2007: The Missing Manual by Bonnie Biafore Microsoft Project 2010: The Missing Manual by Bonnie Biafore Netbooks: The Missing Manual by J.D Biersdorfer Office 2007: The Missing. .. QuickBooks 2010: The Missing Manual by Bonnie Biafore QuickBooks 2011: The Missing Manual by Bonnie Biafore Quicken 2009: The Missing Manual by Bonnie Biafore Switching to the Mac: The Missing Manual, Leopard Edition by David Pogue Switching to the Mac: The Missing Manual, Snow Leopard Edition by David Pogue Wikipedia: The Missing Manual by John Broughton Windows XP Home Edition: The Missing Manual, Second... FileMaker Pro 11: The Missing Manual by Susan Prosser and Stuart Gripman Flash CS4: The Missing Manual by Chris Grover with E.A Vander Veer Flash CS5: The Missing Manual by Chris Grover Google Apps: The Missing Manual by Nancy Conner The Internet: The Missing Manual by David Pogue and J.D Biersdorfer iMovie ’08 & iDVD: The Missing Manual by David Pogue iMovie ’09 & iDVD: The Missing Manual by David Pogue... The Missing Manual by Chris Grover, Matthew MacDonald, and E.A Vander Veer Office 2010: The Missing Manual by Nancy Connor, Chris Grover, and Matthew MacDonald Office 2008 for Macintosh: The Missing Manual by Jim Elferdink Palm Pre: The Missing Manual by Ed Baig PCs: The Missing Manual by Andy Rathbone Personal Investing: The Missing Manual by Bonnie Biafore Photoshop CS4: The Missing Manual by Lesa... Photoshop CS5: The Missing Manual by Lesa Snider Photoshop Elements 7: The Missing Manual by Barbara Brundage Photoshop Elements 8 for Mac: The Missing Manual by Barbara Brundage Photoshop Elements 8 for Windows: The Missing Manual by Barbara Brundage PowerPoint 2007: The Missing Manual by E.A Vander Veer Premiere Elements 8: The Missing Manual by Chris Grover QuickBase: The Missing Manual by Nancy... perfectly flat without the assistance of weights or cinder blocks Recent and upcoming titles include: Access 2007: The Missing Manual by Matthew MacDonald Access 2010: The Missing Manual by Matthew MacDonald Buying a Home: The Missing Manual by Nancy Conner CSS: The Missing Manual, Second Edition, by David Sawyer McFarland Creating a Web Site: The Missing Manual, Second Edition, by Matthew MacDonald David... two versions of the tools on your hard drive During the install process, click the Developer folder icon and select Other to choose the location for the beta version 4 Navigate to the root of your hard drive by selecting its name from the list of DEVICES Then click the New Folder button and type DeveloperBeta Click Create to create the folder 16 iPhone App Development: The Missing Manual www.it-ebooks.info . David Pogue iPhone App Development: The Missing Manual by Craig Hockenberry iPhoto ’08: The Missing Manual by David Pogue iPhoto ’09: The Missing Manual by. Vista: The Missing Manual by David Pogue Windows 7: The Missing Manual by David Pogue Word 2007: The Missing Manual by Chris Grover Your Body: The Missing Manual