7† doing data books in › download › 0000 › 5769 › ... · 7† doing data in1 books †...

30
Neal Goldstein Tony Bove 7 IN 1 BOOKS BOOKS • Creating the Killer App • Objective-C ® and iPhone Technologies • Building a Utility App • Debugging and Tuning • Building an Industrial-Strength App • Storing and Accessing Data • Extending the App to Ultracool iPhone ® Application Development ALL-IN-ONE Making Everything Easier!

Upload: others

Post on 10-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Neal GoldsteinTony Bove

7 IN 1BOOKSBOOKS

• Creating the Killer App• Objective-C® and iPhone Technologies• Building a Utility App• Debugging and Tuning• Building an Industrial-Strength App• Storing and Accessing Data• Extending the App to Ultracool

iPhone® Application

DevelopmentA L L - I N - O N E

Making Everything Easier!™

Visit the companion Web site at www.dummies.com/go/

iphoneappdevaio to find plenty of code examples you

can use in developing your own cool iPhone apps!

Open the book and find:

• A detailed example of a real app and how it’s built

• How to keep your users happy

• What Core Data is and how to use it

• Great tips for marketing your apps

• How Xcode® helps you debug and fine-tune your code

• Ways to build e-mail functionality into your app

• Development techniques using the Software Development Kit

• Rules for managing memory

Neal Goldstein is an iPhone developer and the author of Objective-C For

Dummies. His iPhone startup is developing an application that will radically

change how iPhones are used to manage information. Tony Bove is the

creator of Tony’s Tips for iPhone Users and wrote or cowrote all editions

of iPod & iTunes For Dummies.

$39.99 US / $47.99 CN / £27.99 UK

ISBN 978-0-470-54293-4

Macintosh/Apple Programming

Go to Dummies.com®

for videos, step-by-step examples, how-to articles, or to shop!

Build killer iPhone apps, sell them in the App Store,and have fun doing it!We could have called this the complete guide to iPhone and iPod touch app development, because it’s packed with more than 800 pages covering what you need to know to create and sell the best iPhone apps ever! Whether you’re a novice developer or a seasoned pro, you’ll benefit from this good advice about what makes a great mobile app, easy ways to code, how to market your stuff, and more.

• Think small — understand what makes a great app for a mobile device and how to play to the iPhone’s strengths

• Know the code — get familiar with Objective-C, object-oriented programming, and the extensions that apply to iPhone development

• Functional user interfaces — learn how to design, build, and implement an effective user interface

• Industrial-strength apps — explore how to work with table views, controllers and their models, and maps

• Doing data — look at ways to store and access data on the iPhone and how Core Data can make programming easier

Creatin

g the

Killer A

pp

Ob

jective-C® an

d

iPho

ne Tech

no

log

ies

Build

ing a U

tility Ap

p

Deb

ug

gin

g and

Tu

nin

g

Buildin

g an Ind

ustrial- Stren

gth A

pp

Storin

g and

Accessin

g Data

Extend

ing th

e Ap

pto U

ltracoo

l

iPhone® A

pplicationD

evelopment

AL

L-IN

-ON

EGoldstein

Bove

spine=1.776”

Page 2: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

spine=1.773”

Start with FREE Cheat SheetsCheat Sheets include • Checklists • Charts • Common Instructions • And Other Good Stuff!

Get Smart at Dummies.com Dummies.com makes your life easier with 1,000s of answers on everything from removing wallpaper to using the latest version of Windows.

Check out our • Videos • Illustrated Articles • Step-by-Step Instructions

Plus, each month you can win valuable prizes by entering our Dummies.com sweepstakes. *

Want a weekly dose of Dummies? Sign up for Newsletters on • Digital Photography • Microsoft Windows & Office • Personal Finance & Investing • Health & Wellness • Computing, iPods & Cell Phones • eBay • Internet • Food, Home & Garden

Find out “HOW” at Dummies.com

*Sweepstakes not currently available in all countries; visit Dummies.com for official rules.

Get More and Do More at Dummies.com®

To access the Cheat Sheet created specifically for this book, go to www.dummies.com/cheatsheet/iphoneapplicationdevelopmentaio

Page 3: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone® Application Development

A L L - I N - O N E

FOR

DUMmIES‰

01_542934-ffirs.indd i01_542934-ffirs.indd i 3/23/10 10:54 PM3/23/10 10:54 PM

Page 4: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

01_542934-ffirs.indd ii01_542934-ffirs.indd ii 3/23/10 10:54 PM3/23/10 10:54 PM

Page 5: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

by Neal Goldstein and Tony Bove

iPhone® Application Development

A L L - I N - O N E

FOR

DUMmIES‰

01_542934-ffirs.indd iii01_542934-ffirs.indd iii 3/23/10 10:54 PM3/23/10 10:54 PM

Page 6: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone® Application Development All-in-One For Dummies®

Published byWiley Publishing, Inc.111 River StreetHoboken, NJ 07030-5774

www.wiley.com

Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana

Published by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates in the United States and other countries, and may not be used without written permission. iPhone is a registered trademark of Apple, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITH-OUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZA-TION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.

For technical support, please visit www.wiley.com/techsupport.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

Library of Congress Control Number: 2010924562

ISBN: 978-0-470-54293-4

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

01_542934-ffirs.indd iv01_542934-ffirs.indd iv 3/23/10 10:54 PM3/23/10 10:54 PM

Page 7: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

About the AuthorsNeal Goldstein is a recognized leader in making state-of-the-art and cutting-edge technologies practical for commercial and enterprise development. He was one of the fi rst technologists to work with commercial developers at fi rms such as Apple Computer, Lucasfi lm, and Microsoft to develop com-mercial applications using object-based programming technologies. He was a pioneer in moving that approach into the corporate world for developers at Liberty Mutual Insurance, USWest (now Verizon), National Car Rental, EDS, and Continental Airlines, showing them how object-oriented programming could solve enterprise-wide problems. His book (with Jeff Alger) on object-oriented development, Developing Object-Oriented Software for the Macintosh (Addison Wesley, 1992), introduced the idea of scenarios and patterns to developers. He was an early advocate of the Microsoft .NET framework, and successfully intro-duced it into many enterprises, including Charles Schwab. He was one of the earliest developers of Service Oriented Architecture (SOA), and as Senior Vice President of Advanced Technology and the Chief Architect at Charles Schwab, he built an integrated SOA solution that spanned the enterprise, from desktop PCs to servers to complex network mainframes. (He holds three patents as a result.) As one of IBM’s largest customers, he introduced them to SOA at the enterprise level and encouraged them to head in that direction. He is currently leading an iPhone startup that is developing an application that will radically change how people can use iPhones to manage information.

Tony Bove is crazy about the iPhone, and not only provides free tips on his Web site (www.tonybove.com) but also took the plunge to develop an iPhone application (Tony’s Tips for iPhone Users). Tony has written more than two dozen books on computing, desktop publishing, and multimedia, including iPod and iTunes For Dummies (Wiley), Just Say No to Microsoft (No Starch Press), The Art of Desktop Publishing (Bantam), and a series of books about Macromedia Director, Adobe Illustrator, and PageMaker. Tony founded Desktop Publishing/Publish magazine and the Inside Report on New Media newsletter, and wrote the weekly Macintosh column for Computer Currents for a decade, as well as articles for NeXTWORLD, the Chicago Tribune Sunday Technology Section, and NewMedia. Tracing the personal computer revolu-tion back to the 1960s counterculture, Tony produced a CD-ROM interactive documentary in 1996, Haight-Ashbury in the Sixties (featuring music from the Grateful Dead, Janis Joplin, and Jefferson Airplane). He also developed the Rockument music site, www.rockument.com, with commentary and podcasts focused on rock music history. As a founding member of the Flying Other Brothers, which toured professionally and released three commercial CDs, Tony performed with Hall of Fame rock musicians. Tony has also worked as a director of enterprise marketing for leading-edge software companies, as a marketing messaging consultant, and as a communications director and technical publications manager.

01_542934-ffirs.indd v01_542934-ffirs.indd v 3/23/10 10:54 PM3/23/10 10:54 PM

Page 8: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

DedicationNeil Goldstein: To my wife, Linda. Without her, I never would have been able to write four books in the last year. She deserves special recognition for her support, patience, and maintaining her (and my) sense of humor. I’ve got to be the luckiest guy in the world. Thank you.

Tony Bove: Tony dedicates this book to his sons, nieces, nephews, their cousins, and all their children . . . the iPhone generation.

Authors’ AcknowledgmentsNeil Goldstein: Thanks to my friend Jeff Elias for the San Francisco photo-graphs used in the RoadTrip application. Also thanks to my business part-ners, Jeff Elias and Jeff Enderwick, for their support and picking up the slack while I was engaged in fi nishing this book. Carole Jelen, agent extraordinaire, helped keep things together during some interesting challenges.

Senior Acquisitions Editor Katie Feltman was a delight to work with, as always. Her management skills and in-depth understanding of our readers’ needs really helped focus this book. Paul Levesque has got to be the best Project Editor of all times, both because of his skills and his continued will-ingness to put up with me. Copy Editor Virginia Sanders did a great job in helping me make things clearer. Technical reviewer Glenda Adams, yet again, added a great second pair of eyes.

Tony Bove: Tony owes thanks and a happy hour or two to Carole Jelen at Waterside for agenting, to Maggie Canon for putting the authors together, and to Kathy Pennington for support.

01_542934-ffirs.indd vi01_542934-ffirs.indd vi 3/23/10 10:54 PM3/23/10 10:54 PM

Page 9: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

01_542934-ffirs.indd vii01_542934-ffirs.indd vii 3/23/10 10:54 PM3/23/10 10:54 PM

Page 10: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Publisher’s Acknowledgments

We’re proud of this book; please send us your comments at http://dummies.custhelp.com. For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.

Some of the people who helped bring this book to market include the following:

Acquisitions, Editorial, and Media

Development

Senior Project Editor: Paul Levesque

Acquisitions Editor: Katie Feltman

Copy Editor: Virginia Sanders

Technical Editor: Glenda Adams

Editorial Manager: Leah Cameron

Media Development Project Manager: Laura Moss-Hollister

Media Development Assistant Project

Manager: Jenny Swisher

Media Development Associate Producers: Josh Frank, Marilyn Hummel, Douglas Kuhn, and Shawn Patrick

Editorial Assistant: Amanda Graham

Sr. Editorial Assistant: Cherie Case

Cartoons: Rich Tennant (www.the5thwave.com)

Composition Services

Project Coordinator: Katherine Crocker

Layout and Graphics: Amy Hassos, Joyce Haughey, Christine Williams

Proofreaders: Laura Albert, Evelyn C. Gibson

Indexer: Rebecca R. Plunkett

Publishing and Editorial for Technology Dummies

Richard Swadley, Vice President and Executive Group Publisher

Andy Cummings, Vice President and Publisher

Mary Bednarek, Executive Acquisitions Director

Mary C. Corder, Editorial Director

Publishing for Consumer Dummies

Diane Graves Steele, Vice President and Publisher

Composition Services

Debbie Stailey, Director of Composition Services

01_542934-ffirs.indd viii01_542934-ffirs.indd viii 3/23/10 10:54 PM3/23/10 10:54 PM

Page 11: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Contents at a GlanceIntroduction ................................................................ 1

Book I: Creating the Killer App ..................................... 9Chapter 1: What Makes a Killer iPhone App ................................................................ 11Chapter 2: Creating a Compelling User Experience .................................................... 25Chapter 3: Enlisting in the Developer Corps ................................................................ 43Chapter 4: Getting to Know the SDK ............................................................................. 57Chapter 5: Looking Behind the Screen .......................................................................... 77Chapter 6: Death, Taxes, and iPhone Provisioning ..................................................... 95Chapter 7: The App Store Is Not Enough .................................................................... 117

Book II: Objective-C and iPhone Technologies ............ 133Chapter 1: Using Objective-C’s Extensions to C for iPhone Development ............. 135Chapter 2: The Real Truth about Object-Oriented Programming ........................... 167Chapter 3: Digging Deeper Into Objective-C ............................................................... 195Chapter 4: Leveraging the Foundation Framework in Your App ............................. 227Chapter 5: Getting Other Objects to Do Your Work for You .................................... 257

Book III: Building a Utility App — DeepThoughts ...... 269Chapter 1: Understanding How an App Runs ............................................................. 271Chapter 2: Understanding the User Interface Controls ............................................ 287Chapter 3: Developing the Main View ......................................................................... 305Chapter 4: Developing the Flipside Controls ............................................................. 319Chapter 5: Extending Your App with a Photo View ................................................... 351

Book IV: Debugging and Tuning Your Application....... 379Chapter 1: Using the Xcode Debugger ........................................................................ 381Chapter 2: Tuning Your Code Using Xcode’s Instruments Application .................. 401

Book V: Building an Industrial Strength Application — RoadTrip! ......................................... 425Chapter 1: Designing Your Application ....................................................................... 427Chapter 2: Setting the Table ......................................................................................... 439Chapter 3: Navigating .................................................................................................... 467

02_542934-ftoc.indd ix02_542934-ftoc.indd ix 3/23/10 10:51 PM3/23/10 10:51 PM

Page 12: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Chapter 4: Creating Controllers and Model Classes ................................................. 495Chapter 5: Finding Your Way ....................................................................................... 531Chapter 6: Geocoding and Reverse Geocoding .......................................................... 555

Book V I: Storing and Accessing Data ........................ 577Chapter 1: Show Me the Data ....................................................................................... 579Chapter 2: A More Flexible Generic Controller .......................................................... 619Chapter 3: Working the URL Loading System ............................................................ 635Chapter 4: Setting Up Core Data .................................................................................. 669Chapter 5: Putting Core Data to Work ......................................................................... 705

Book V II: Extending the App to the Realm of Ultracool ................................................... 739Chapter 1: A User Interface for Adding Hotels and Using the Address Book ........ 741Chapter 2: Incorporating E-Mail: Postcards from the Road ..................................... 787Chapter 3: Are We There Yet? ...................................................................................... 805

Index ...................................................................... 823

02_542934-ftoc.indd x02_542934-ftoc.indd x 3/23/10 10:51 PM3/23/10 10:51 PM

Page 13: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Table of Contents

Introduction ................................................................. 1About This Book .............................................................................................. 2Conventions Used in This Book ..................................................................... 3Foolish Assumptions ....................................................................................... 3How This All-in-One Book Is Organized ........................................................ 4

Book I: Creating the Killer App ............................................................. 4Book II: Objective-C and iPhone Technologies .................................. 4Book III: Building a Utility App — DeepThoughts .............................. 5Book IV: Debugging and Tuning Your Application ............................ 5Book V: Building an Industrial Strength Application —

RoadTrip! ............................................................................................. 5Book VI: Storing and Accessing Data................................................... 6Book VII: Extending the App to the Realm of Ultracool .................... 6

Icons Used in This Book ................................................................................. 6Where to Go from Here ................................................................................... 7

Book I: Creating the Killer App ...................................... 9

Chapter 1: What Makes a Killer iPhone App. . . . . . . . . . . . . . . . . . . . . .11

Figuring Out What Makes a Great iPhone Application ............................. 12Exploiting the Platform ................................................................................. 14

Device-guided design........................................................................... 14Exploiting the features ........................................................................ 14

Embracing the iPhone’s Limitations ........................................................... 17Living with the small screen ............................................................... 17Designing for fi ngers ............................................................................ 17Balancing limited computer power, memory, and battery life ...... 18

Why Develop iPhone Applications? ............................................................ 19Developing with Apple’s Expectations in Mind ......................................... 20An Overview of the Development Cycle ..................................................... 20The Sample Applications .............................................................................. 22What’s Next .................................................................................................... 23

Chapter 2: Creating a Compelling User Experience . . . . . . . . . . . . . . .25

Deep Thoughts on the User Experience ..................................................... 26Creating Compelling Content ....................................................................... 27

Focusing on the task at hand ............................................................. 27Maintaining consistency with the user’s world ............................... 28Modeling actions on the real world ................................................... 29

02_542934-ftoc.indd xi02_542934-ftoc.indd xi 3/23/10 10:51 PM3/23/10 10:51 PM

Page 14: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone Application Development All-in-One For Dummiesxii

Designing the User Interface — Form Follows Function .......................... 31Consistency across apps is a good thing .......................................... 31Making it obvious ................................................................................. 32Engaging the user................................................................................. 33

Designing the User Experience .................................................................... 34Understanding the real-world context .............................................. 34Doing it better on the iPhone ............................................................. 36

Playing to the iPhone Strengths .................................................................. 36Knowing the location of the device ................................................... 37Accessing the Internet......................................................................... 37Recording and playing content .......................................................... 38Tracking orientation and motion ....................................................... 38

Incorporating the Device Context ............................................................... 38Avoiding Practices that Get Apps Rejected ............................................... 39

Chapter 3: Enlisting in the Developer Corps . . . . . . . . . . . . . . . . . . . . . .43

Becoming a Registered iPhone Developer ................................................. 44Joining the iPhone Developer Program ...................................................... 46Exploring the iPhone Dev Center ................................................................ 51

Looking forward to using the SDK ..................................................... 52Resources in the iPhone Dev Center ................................................. 53

Downloading the SDK .................................................................................... 54Getting Yourself Ready for the SDK ............................................................ 56

Chapter 4: Getting to Know the SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57

Developing Using the SDK ............................................................................ 57Creating Your Xcode Project ....................................................................... 58Exploring Your Project ................................................................................. 60Building and Running Your Application ..................................................... 65The iPhone Simulator .................................................................................... 68

Hardware interaction .......................................................................... 68Gestures ................................................................................................ 68Uninstalling apps and resetting your device .................................... 69Limitations ............................................................................................ 70

Customizing Xcode to Your Liking .............................................................. 71Using Interface Builder ................................................................................. 73It’s Time to Explore ....................................................................................... 76

Chapter 5: Looking Behind the Screen. . . . . . . . . . . . . . . . . . . . . . . . . . .77

Using Frameworks ......................................................................................... 77Using Design Patterns ................................................................................... 78

The Model-View-Controller (MVC) pattern ...................................... 79The MVC in action ............................................................................... 81

02_542934-ftoc.indd xii02_542934-ftoc.indd xii 3/23/10 10:51 PM3/23/10 10:51 PM

Page 15: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Table of Contents xiii

Working with Windows and Views .............................................................. 82Looking out the window...................................................................... 82Admiring the view ................................................................................ 83The kinds of views you use................................................................. 85

Controlling View Controllers ....................................................................... 87What About the Model? ................................................................................ 89Adding Your Own Application’s Behavior ................................................. 90

The Delegation pattern ....................................................................... 91The Target-Action pattern .................................................................. 92

Doing What When? ........................................................................................ 93Whew! .............................................................................................................. 93

Chapter 6: Death, Taxes, and iPhone Provisioning . . . . . . . . . . . . . . . .95

How the Process Works ................................................................................ 96The Distribution process .................................................................... 96The Development process .................................................................. 97

Provisioning Your Device for Development ............................................... 98Creating Your Development Provisioning Profi le and Development

Certifi cate .................................................................................................. 100Provisioning Your Application for the App Store ................................... 104Using iTunes Connect to Manage Apps in the App Store ....................... 106Supplying the Information Required for the App Store .......................... 108

Adding the metadata and artwork ................................................... 109You’re not done yet ........................................................................... 112Uploading your information ............................................................. 112Uploading your app and its data...................................................... 113

Avoiding the App Store Rejection Slip ...................................................... 114Now What? .................................................................................................... 116

Chapter 7: The App Store Is Not Enough . . . . . . . . . . . . . . . . . . . . . . . .117

Why People Buy Apps from the App Store .............................................. 118Finding out how to reach your potential customers ..................... 118Marketing 101: Pricing your app ...................................................... 120

Knowing Your Customers ........................................................................... 122Tracking downloads .......................................................................... 122Adding analytical code to your app ................................................ 124Putting ads in your app ..................................................................... 124

Deploying the In App Purchase Feature ................................................... 125Links Are Not Enough ................................................................................. 128

Using iTunes affi liate links ................................................................ 128Making use of user reviews .............................................................. 129Going social ........................................................................................ 129Updating your app for attention ...................................................... 131Buying advertising and publicity ..................................................... 131

02_542934-ftoc.indd xiii02_542934-ftoc.indd xiii 3/23/10 10:51 PM3/23/10 10:51 PM

Page 16: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone Application Development All-in-One For Dummiesxiv

Book II: Objective-C and iPhone Technologies ............. 133

Chapter 1: Using Objective-C Extensions to C’s for iPhone Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135

What You Need to Know About Objective-C ............................................ 136Introducing Objects and Classes ............................................................... 137

Grasping objects and their classes .................................................. 139Scoping instance variables ............................................................... 140Spreading the wealth across fi les .................................................... 141Knowing the naming conventions ................................................... 142Using id and nil ................................................................................... 142

Declaring the Class Interface ..................................................................... 143The @interface compiler directive and fi rst line............................ 144The instance variables ...................................................................... 144The methods....................................................................................... 145The @end compiler directive ........................................................... 147

The Implementation — Coding the Methods ........................................... 148The @implementation compiler directive ...................................... 148Defi ning the methods ........................................................................ 148The @end compiler directive ........................................................... 149

Allocating Objects ....................................................................................... 149All objects are dynamic..................................................................... 150Initialization ........................................................................................ 151

Sending Messages to Your Objects ........................................................... 152Working with Declared Properties ............................................................ 153

Adding properties .............................................................................. 154Accessing the properties from within the class ............................ 157Releasing the object assigned to a property .................................. 157Using accessor methods to get data from objects ........................ 158Properly using properties ................................................................. 162

Extending the Program ............................................................................... 163Inheritance .......................................................................................... 164Delegation ........................................................................................... 165

Chapter 2: The Real Truth about Object-Oriented Programming. . . .167

Not All Modules Are Created Equal ........................................................... 168Understanding How Objects Behave ........................................................ 168Seeing the Concepts in Action ................................................................... 170

Encapsulation ..................................................................................... 170Polymorphism .................................................................................... 171

How Inheritance Works .............................................................................. 172Knowing what inheritance enables you to do ................................ 176Using inheritance effectively ............................................................ 177

Implementing Inheritance in a Program ................................................... 178Deriving classes ................................................................................. 178Replacing a control structure with polymorphism ....................... 184The fi nal word on polymorphism and inheritance ........................ 186

02_542934-ftoc.indd xiv02_542934-ftoc.indd xiv 3/23/10 10:51 PM3/23/10 10:51 PM

Page 17: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Table of Contents xv

Encapsulating Objects ................................................................................ 187Getting to know the Model-View-Controller

(MVC) pattern ................................................................................. 187Implementing the MVC pattern ........................................................ 189

Looking Ahead ............................................................................................. 193

Chapter 3: Digging Deeper Into Objective-C . . . . . . . . . . . . . . . . . . . . .195

Initializing Objects ....................................................................................... 195Invoking the superclass’s init method ............................................ 197Initializing instance variables ........................................................... 199Returning self ..................................................................................... 200

Raising and Terminating Responsible Objects ........................................ 206Understanding the object lifecycle .................................................. 207Using reference counting .................................................................. 208

Running the Static Analyzer ....................................................................... 212Plugging the Leaks ....................................................................................... 215Attending to Memory Management Subtleties — Arrays and

Autorelease ............................................................................................... 221Considering objects in arrays .......................................................... 221Understanding autorelease .............................................................. 222Using the autorelease pool ............................................................... 223

Some Basic Memory Management Rules You Shouldn’t Forget ............ 225

Chapter 4: Leveraging the Foundation Framework in Your App . . . .227

Useful Utility Classes ................................................................................... 228Container Classes ....................................................................................... 229Taking Advantage of Array Objects .......................................................... 230

Tiptoeing through an array .............................................................. 232Working with fi xed arrays ................................................................. 233Using C arrays .................................................................................... 234Expanding to multidimensional arrays ........................................... 234

Using Dictionaries ....................................................................................... 236Understanding a dictionary’s keys and values .............................. 236Creating a dictionary ......................................................................... 237

Using Property Lists .................................................................................... 238Working with property lists.............................................................. 239Adding a property list to your project ............................................ 240Using plists ......................................................................................... 242Creating a mutable dictionary .......................................................... 245Creating, initializing, and using the symbol string ........................ 245

Dictionaries of Dictionaries ........................................................................ 246Managing a dictionary of dictionaries ............................................. 250

Modifying the Property List ....................................................................... 250Adding a new entry to the property list.......................................... 251Updating the dictionary .................................................................... 253

02_542934-ftoc.indd xv02_542934-ftoc.indd xv 3/23/10 10:51 PM3/23/10 10:51 PM

Page 18: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone Application Development All-in-One For Dummiesxvi

Chapter 5: Getting Other Objects to Do Your Work for You . . . . . . . .257

Understanding Delegation ......................................................................... 258Using Protocols ............................................................................................ 258

Declaring a protocol .......................................................................... 259Adopting a protocol........................................................................... 260Adding delegation to Transaction ................................................... 261

Categories .................................................................................................... 264Using categories ................................................................................. 267Defi ning informal protocols .............................................................. 267

Book III: Building a Utility App — DeepThoughts ....... 269

Chapter 1: Understanding How an App Runs . . . . . . . . . . . . . . . . . . . .271

App Anatomy 101 — The Lifecycle ........................................................... 271It all starts with the main nib fi le ..................................................... 274Initialization ........................................................................................ 277

Termination .................................................................................................. 279Other Runtime Considerations .................................................................. 280

Responding to interruptions ............................................................ 280Seeing how memory management

works on the iPhone ...................................................................... 282Whew! ............................................................................................................ 285

Chapter 2: Understanding the User Interface Controls . . . . . . . . . . . .287

Running the Utility Application Template ............................................... 287Inspecting the Main View ........................................................................... 288

Initializing the Main view .................................................................. 289Controller a subset of UIViewController ......................................... 291Main view a subset of UIView ........................................................... 291

Inspecting the Flipside View ...................................................................... 292MainViewController initializes the Flipside view .......................... 293Flipside view a subset of UIView ...................................................... 295

Unraveling the Info and Done Buttons ..................................................... 295Info connects to showinfo ................................................................ 296Done sends an action to FlipsideViewController .......................... 299

Understanding Event Processing .............................................................. 302

Chapter 3: Developing the Main View . . . . . . . . . . . . . . . . . . . . . . . . . .305

Using the Xcode Text Editor ...................................................................... 305Accessing Documentation .......................................................................... 306

Quick Help .......................................................................................... 306The header fi le for a symbol ............................................................. 307Documentation window .................................................................... 307

02_542934-ftoc.indd xvi02_542934-ftoc.indd xvi 3/23/10 10:51 PM3/23/10 10:51 PM

Page 19: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Table of Contents xvii

Help menu ........................................................................................... 309Find ...................................................................................................... 309

Where Does Your Code Go? ....................................................................... 311The delegate object ........................................................................... 311The controller object ........................................................................ 311

Creating an Animated View ........................................................................ 312The viewDidLoad method ................................................................. 315Drawing the view ............................................................................... 316The animation block .......................................................................... 317

Testing the Main View ................................................................................. 318

Chapter 4: Developing the Flipside Controls. . . . . . . . . . . . . . . . . . . . .319

Respecting User Preferences ..................................................................... 319Setting Up User Preferences ....................................................................... 320

Identifying preferences for NSUserDefaults ................................... 321Adding a Constants.h fi le .................................................................. 322

Reading Preferences into the App ............................................................. 324Setting Up Preferences for the Settings App ............................................ 327

Adding a Settings bundle to your project ....................................... 327Setting up the property list .............................................................. 328Testing your app with the Settings app .......................................... 332

Setting Up Preferences in the Flipside View ............................................ 334Objective-C properties ...................................................................... 336Adding methods for the interface objects ...................................... 337

Connecting the Interface Objects in Interface Builder ........................... 344

Chapter 5: Extending Your App with a Photo View . . . . . . . . . . . . . . .351

Adding a Second View ................................................................................. 352Initializing view controllers in the app delegate ............................ 352Discovering how horizontal scrolling works .................................. 355Playing the memory management game ......................................... 355

Implementing the Photo View .................................................................... 356Creating subclasses of UIView and UIViewController ................... 356Creating a nib fi le for the Photo view .............................................. 359Modifying the window nib fi le for scrolling .................................... 364

Animating the Photo View .......................................................................... 366Reusing MainViewController code for PhotoViewController ...... 366Adding a default image ...................................................................... 368

Accessing Media on the iPhone ................................................................. 370Adding an Application Icon ........................................................................ 376A Lot Accomplished Very Quickly ............................................................. 377

02_542934-ftoc.indd xvii02_542934-ftoc.indd xvii 3/23/10 10:51 PM3/23/10 10:51 PM

Page 20: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone Application Development All-in-One For Dummiesxviii

Book IV: Debugging and Tuning Your Application ....... 379

Chapter 1: Using the Xcode Debugger . . . . . . . . . . . . . . . . . . . . . . . . . .381

Understanding Bugs .................................................................................... 382Using the Debugger ..................................................................................... 385

Debugging in the Text editor ............................................................ 386Using the Debugger window ............................................................. 390Using the Mini Debugger ................................................................... 392Using the Console .............................................................................. 392

Using Breakpoints ....................................................................................... 394Using the Static Analyzer ............................................................................ 396

Chapter 2: Tuning Your Code Using Xcode’s Instruments Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401

Getting Started with the Instruments Application .................................. 402A Leaks instrument preview ............................................................. 405Actually using the Leaks instrument ............................................... 407

Yes Virginia, There Are Zombies Amongst Us ......................................... 414

Book V: Building an Industrial Strength Application — RoadTrip! .......................................... 425

Chapter 1: Designing Your Application . . . . . . . . . . . . . . . . . . . . . . . . .427

Defi ning the Problems ................................................................................. 427Creating the Program Architecture ........................................................... 433

Models ................................................................................................. 433Views ................................................................................................... 435Models yet again ............................................................................... 435View controllers ................................................................................. 436Stored data mode, saving state, and localization .......................... 436

Understanding the Iterative Nature of the Process ................................ 436

Chapter 2: Setting the Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .439

Understanding Table Views ....................................................................... 439Creating the Table View .............................................................................. 441Creating and Formatting a Grouped Table View ..................................... 442Making UITableViewController Work for You ......................................... 446

Adding sections .................................................................................. 446Adding titles for the sections ........................................................... 452Localization ........................................................................................ 452

Creating the Row Model ............................................................................. 455

02_542934-ftoc.indd xviii02_542934-ftoc.indd xviii 3/23/10 10:51 PM3/23/10 10:51 PM

Page 21: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Table of Contents xix

Seeing How Cells Work ............................................................................... 459Using vanilla cell objects .................................................................. 460Adding subviews to a cell’s Content view ...................................... 460Creating a custom subclass UITableViewCell ................................ 461

Creating the Cell .......................................................................................... 461And Now . . . ................................................................................................. 466

Chapter 3: Navigating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .467

Working with User Selections .................................................................... 467Navigating the navigation controller ............................................... 469Implementing the selection .............................................................. 472

Saving and Restoring State ......................................................................... 475Saving state information ................................................................... 476Restoring the state............................................................................. 479

Respecting User Preferences ..................................................................... 482Adding a Settings bundle to your project ....................................... 483Setting up the property list .............................................................. 484

Reading Settings in the Application .......................................................... 487Using Preferences in Your Application ..................................................... 490

Chapter 4: Creating Controllers and Model Classes . . . . . . . . . . . . . .495

Specifying the Content ................................................................................ 496Creating the View Controller, Nib, and Model Files ................................ 499

Adding the controller and nib fi le .................................................... 499Setting up the nib fi le......................................................................... 501

Getting Trippy with the Trip Model .......................................................... 504Working with controllers .................................................................. 507Managing links in Web view Web sites ........................................... 512Keeping users from going astray ..................................................... 515

Implementing CarInformation ................................................................... 518Setting up the view ............................................................................ 520

Implementing CarServicingInformation .................................................... 523Setting up the view ............................................................................ 523

What’s with the Trip Model and All That Indirection? ........................... 528What’s Next? ................................................................................................. 530

Chapter 5: Finding Your Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .531

Building Your Map Functionality ............................................................... 531Setting up the nib fi le......................................................................... 535

Putting MapKit through Its Paces .............................................................. 538MKMapView ........................................................................................ 538Enhancing the map ............................................................................ 539Adding annotations ........................................................................... 547Going to the current location ........................................................... 552

And There’s Even More .............................................................................. 554

02_542934-ftoc.indd xix02_542934-ftoc.indd xix 3/23/10 10:51 PM3/23/10 10:51 PM

Page 22: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone Application Development All-in-One For Dummiesxx

Chapter 6: Geocoding and Reverse Geocoding . . . . . . . . . . . . . . . . . .555

Reverse Geocoding ...................................................................................... 555(Forward) Geocoding .................................................................................. 559

Adding the FindLocationController and nib fi le ............................ 562What’s Next? ................................................................................................. 576

Book V I: Storing and Accessing Data ......................... 577

Chapter 1: Show Me the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .579

Seeing the Sights .......................................................................................... 579Starting with the Table View ...................................................................... 581

Adding the controller ........................................................................ 582Setting up the controller ................................................................... 583Creating the SightListController in the RootViewController ....... 585

Seeing the Sights .......................................................................................... 587Sight.plist Is a Plist ...................................................................................... 589

Working with property lists.............................................................. 590Adding a plist to Your Project ................................................................... 592

Adding images .................................................................................... 599Initializing the Sight Objects with plist Data ............................................ 601

Categories and extensions ................................................................ 603Loading the Sights ............................................................................. 604Tiptoeing through an array .............................................................. 606Using dictionaries .............................................................................. 608The Sight object initializes itself with the plist dictionary ........... 610

Displaying the Sights in the SightListController Table View ................. 611Drawing the Thumbs ................................................................................... 614Compile and Run RoadTrip ........................................................................ 616Houston, We Have a Problem . . . .............................................................. 616

The problem being . . . ...................................................................... 617

Chapter 2: A More Flexible Generic Controller . . . . . . . . . . . . . . . . . .619

Seeing How the Old School Generic Controller Worked ....................... 619Stepping through the process .......................................................... 620The problem being . . . ...................................................................... 622

Understanding Delegation ......................................................................... 623Naming conventions .......................................................................... 624

Using Protocols ............................................................................................ 624Declaring a protocol .......................................................................... 624Adding delegation to WebViewController ...................................... 625Changing the way WebViewController is instantiated and

initialized ......................................................................................... 626Adopting a protocol........................................................................... 630

Asynchronous File Loading ........................................................................ 632

02_542934-ftoc.indd xx02_542934-ftoc.indd xx 3/23/10 10:51 PM3/23/10 10:51 PM

Page 23: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Table of Contents xxi

Chapter 3: Working the URL Loading System . . . . . . . . . . . . . . . . . . . .635

URL Loading System Overview .................................................................. 635Authentication and credentials........................................................ 636Cookie storage .................................................................................... 637Protocol support ................................................................................ 637Using NSURLConnection — Downloading data

asynchronously .............................................................................. 637One more thing .................................................................................. 645

Implementing Sight with Asynchronous Downloads .............................. 647Sending the right message ................................................................ 648Getting Trip in on the action ............................................................ 652

Adding Delegation and the URL Loading System to the Rest of the Model Objects ............................................................................... 654

What About the Map? ................................................................................. 660Where Are You? ........................................................................................... 668

Chapter 4: Setting Up Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .669

Core Data Basics .......................................................................................... 670Starting with the Objects ............................................................................ 671

Add the Attributes ............................................................................. 676Add relationships............................................................................... 682Generate your classes ....................................................................... 686Managed objects ................................................................................ 692

The Managed Object Context .................................................................... 692The Persistent Store Coordinator and Persistent Store ......................... 693The Core Data Stack .................................................................................... 693

“Coding” the stack ............................................................................. 694Adding the Core Data Framework.................................................... 697Connecting the pieces ....................................................................... 698

What’s in the Core Data Stack .................................................................... 700The Managed Object Context ........................................................... 700The Persistent Store Coordinator .................................................... 701The Managed Object Model.............................................................. 703

Putting Core Data to Work ......................................................................... 704

Chapter 5: Putting Core Data to Work. . . . . . . . . . . . . . . . . . . . . . . . . . .705

Understanding the Fetch Request ............................................................. 705Getting the Ball Rolling ............................................................................... 706Adding HotelController ............................................................................... 713

Setting up the controller ................................................................... 714Repurposing the code from the template ....................................... 715Modifying viewDidLoad .................................................................... 716Creating a fetched results controller .............................................. 717Adding a new hotel ............................................................................ 720Table view methods .......................................................................... 723

02_542934-ftoc.indd xxi02_542934-ftoc.indd xxi 3/23/10 10:51 PM3/23/10 10:51 PM

Page 24: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone Application Development All-in-One For Dummiesxxii

The fetched results controller’s delegate ....................................... 724Managing memory ............................................................................ 725You now have someplace to stay .................................................... 725

Supporting Undo .......................................................................................... 726Supporting undo in HotelController ................................................ 728Keeping tabs on your Undo manager .............................................. 731

Adding Delete ............................................................................................... 735What’s Left .................................................................................................... 738

Book V II: Extending the App to the Realm of Ultracool .............................................................. 739

Chapter 1: A User Interface for Adding Hotels and Using the Address Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .741

Add AddHotelController ............................................................................. 741Adding controls to the view ............................................................. 743Setting up the controller ................................................................... 747

Making the Connections in Interface Builder ........................................... 749Adding Some Code to Actually Add Some Functionality ........................ 754

Dismissing the controller .................................................................. 757Entering and Saving the Hotel Information .............................................. 761

Dismissing the keyboard................................................................... 761Adding Cancel and Save buttons ..................................................... 763Setting up the AddHotelController delegate .................................. 765Adding the delegation plumbing ...................................................... 768Using default data .............................................................................. 769

Interfacing with the Address Book Application ....................................... 773Adding a hotel to your Address Book ............................................. 782

Chapter 2: Incorporating E-Mail: Postcards from the Road . . . . . . . .787

Mail on the iPhone ....................................................................................... 787The usual start to your adventure ................................................... 789Adding the MessageUI framework ................................................... 789

Creating the Interface ................................................................................. 790Selecting the new entry ..................................................................... 792Reusing what you’ve already done in RoadTripController .......... 794

Creating the Mail Message ......................................................................... 796Displaying the view ............................................................................ 798The delegate ....................................................................................... 801

More .............................................................................................................. 803

02_542934-ftoc.indd xxii02_542934-ftoc.indd xxii 3/23/10 10:51 PM3/23/10 10:51 PM

Page 25: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Table of Contents xxiii

Chapter 3: Are We There Yet? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805

Adding Core Location ................................................................................. 806Taking the Accelerometer for a Spin ......................................................... 812

Doing something with the shake ...................................................... 818Yes, You’re Finally There ........................................................................... 821

Index ....................................................................... 823

02_542934-ftoc.indd xxiii02_542934-ftoc.indd xxiii 3/23/10 10:51 PM3/23/10 10:51 PM

Page 26: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

iPhone Application Development All-in-One For Dummiesxxiv

02_542934-ftoc.indd xxiv02_542934-ftoc.indd xxiv 3/23/10 10:51 PM3/23/10 10:51 PM

Page 27: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

Introduction

When Apple introduced the iPhone Software Development Kit (SDK) in 2008, the iPhone and iPod touch suddenly became the handheld

computers they were meant to be. Interest in the SDK exceeded Apple’s expectations — in fact, Apple’s servers supplying the beta for the first SDK download were overwhelmed.

Since then, the App Store has grown to become the repository of over 140,000 applications, which collectively are driving innovation beyond the reach of other mobile devices. The success of the App Store — over three billion downloads — has turned the iPhone into the premier mobile device for publishing software. All the leading mobile applications typically appear on the iPhone first, and as a reader of this book, you may be creating the next one.

As I continue to explore the iPhone as a new platform, I keep finding more possibilities for applications that never existed before. The iPhone or iPod touch is a mobile computer, but not like a mobile desktop. Its hardware and software make it possible to wander the world, or your own neighborhood, and stay connected to whomever and whatever you want to. It enables a new class of here-and-now applications that give you access to content-rich services and let you view information about what’s going on around you and where you are, and interact with those services or with others on the Internet.

One of the hallmarks of a great iPhone application is that it leverages the iPhone’s unique hardware, especially its ability to know where the user is. The iPhone SDK includes tools such as MapKit, which makes it much easier to use the location-based features of the iPhone in an application. MapKit makes it possible for even a beginning developer to take full advantage of the location hardware, and I’ve included the code for an example app (called RoadTrip) to show you how. And the frameworks supplied in the SDK are especially rich and mature. All you really have to do is add your application’s user interface and functionality to the framework, and then “poof” . . . an instant application.

If you’re familiar with the first version of the SDK, you’re in for a pleasant surprise: The latest version of the SDK, which includes Xcode 3.2.2, is a lot, lot better and easier to use. What’s really hard, after you’ve learned your way around the SDK and its various framework, is figuring out how to create a structure for the iPhone application’s data and models for the logic of how

03_542934-intro.indd 103_542934-intro.indd 1 3/23/10 10:53 PM3/23/10 10:53 PM

Page 28: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

2

the application should work. Although there are lots of resources, the prob-lem is exactly that: There are lots of resources — as in thousands of pages of documentation! You may get through a small fraction of the documentation before you just can’t take it anymore and plunge right into coding. Naturally enough, there will be a few false starts and blind alleys until you find your way, but I predict that after reading this book, it will be (pretty much) smooth sailing.

That’s why, when the For Dummies folks asked me to combine all the knowl-edge and wisdom I could muster to write these minibooks on developing software for the iPhone, I jumped at the chance. Here’s the definitive tome that I wish I had for a reference myself when I started developing iPhone software.

Editor’s note: Both authors (Tony and Neal) have published applications for the iPhone — you can find several of Neal’s apps, including ReturnMeTo, in the App Store, along with Tony’s app, Tony’s Tips for iPhone Users.

About This BookiPhone Application Development All-in-One For Dummies is more than just a beginner’s guide to developing iPhone applications — it’s also a concise reference work on programming with Objective-C. And not only do you not need any iPhone development experience to get started, you don’t need any Macintosh development experience either. I expect you to come as a blank slate, ready to be filled with useful information and new ways to do things.

Because of the nature of the iPhone, you can create small, bite-sized appli-cations that can be really powerful. And because you can start small and create real applications that do something important for a user, it’s rela-tively easy to transform yourself from “I know nothing” into a developer who, though not (yet) a superstar, can still crank out quite a respectable application.

But the iPhone can be home to some pretty fancy software as well — so I take you on a journey through building an industrial-strength application and show you the ropes for developing one on your own.

This book distills the hundreds (or even thousands) of pages of Apple docu-mentation, not to mention our own development experiences, into only what’s necessary to start you developing real applications. But this is no recipe book that leaves it up to you to put it all together; rather, it takes you through the frameworks and iPhone architecture in a way that gives you a solid foundation in how applications really work on the iPhone — and acts as a roadmap to expand your knowledge as you need to.

It’s a multicourse banquet, intended to make you feel satisfied (and really full) at the end.

About This Book

03_542934-intro.indd 203_542934-intro.indd 2 3/23/10 10:53 PM3/23/10 10:53 PM

Page 29: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

3

Conventions Used in This BookThis book guides you through the process of building iPhone applications. Throughout, you use the provided iPhone framework classes (and create new ones, of course) and code them with the Objective-C programming language.

Code examples in this book appear in a monospaced font so they stand out a bit better. That means the code looks like this:

#import <UIKit/ UIKit.h>

In addition, code that you need to enter is set in bold, and code you need to delete is set in regular type with a strikethrough, as follows:

#import <UIKit/ UIKit.h> // Add this line to your code

#import <UIKit/ UIKit.h> // Delete this line to your code

Objective-C is based on C, which (I want to remind you) is case-sensitive, so please enter the code that appears in this book exactly as it appears in the text. This book also uses the standard Objective-C naming conventions — for example, class names always start with a capital letter, and the names of methods and instance variables always start with a lowercase letter.

All URLs in this book appear in a monospaced font as well:

www.nealgoldstein.comwww.tonybove.com

If you’re ever uncertain about anything in the code, you can always look at the source code on Neal’s Web site at www.nealgoldstein.com. From time to time, he provides updates for the code there, and posts other things you might find useful. (You can find a mirror image of Neal’s site at www.dummies.com/go/iphoneappdevaio, if you happen to be strolling through the dummies. com site.) Tony offers tips about everything from developing to marketing apps at www.tonybove.com.

Foolish AssumptionsTo begin programming your iPhone applications, you need an Intel-based Macintosh computer with the latest version of the Mac OS on it. (No, you can’t program iPhone applications on the iPhone.) You’ll also need to down-load the iPhone Software Development Kit (SDK) — which is free — but you do have to become a registered iPhone developer before you can do that. (Don’t worry; I show you how in Chapter 3 of Book I.) And, oh yeah, you’ll need an iPhone. You won’t start running your application on it right away — you’ll use the Simulator that Apple provides with the iPhone SDK during the

Foolish Assumptions

03_542934-intro.indd 303_542934-intro.indd 3 3/23/10 10:53 PM3/23/10 10:53 PM

Page 30: 7† Doing data BOOKS IN › download › 0000 › 5769 › ... · 7† Doing data IN1 BOOKS † Creating the Killer App † Objective-C® and iPhone Technologies † Building a Utility

4

initial stages of development — but at some point, you’ll want to test your application on a real, live iPhone (and possibly also an iPod touch).

This book assumes that you have some programming knowledge and that you have at least a passing acquaintance with object-oriented programming, using some variant of the C language (such as C++, C#, or maybe even Objective-C). If not, we point out some resources that can help you get up to speed. The examples in this book are focused on the frameworks that come with the SDK; the code is pretty simple (usually) and straightforward. (I don’t use this book as a platform to dazzle you with fancy coding techniques.)

This book also assumes that you’re familiar with the iPhone itself and that you’ve at least explored Apple’s included applications to get a good working sense of the iPhone look and feel. It would also help if you browse the App Store to see the kinds of applications available there, and maybe even down-load a few free ones (as if I could stop you).

How This All-in-One Book Is OrganizediPhone Application Development All-in-One For Dummies has seven minibooks.

Book I: Creating the Killer AppBook I introduces you to the iPhone world and gives you the fundamental background that you need to develop iPhone applications. You find out what makes a great iPhone application, and how an iPhone application is structured. You also find out how to become an “official” iPhone developer and what you need to do in order to be able to distribute your applications through Apple’s App Store. You find out how to download the Software Development Kit (SDK) — and then you unpack all the goodies contained therein, including Xcode (Apple’s development environment for the OS X operating system) and Interface Builder. (You’ll soon discover that the latter is more than your run-of-the-mill program for building graphical user inter-faces.) I show how everything works together, which should give you a real feel for how an iPhone application works.

Book II: Objective-C and iPhone TechnologiesBook II gives you a rundown on everything you need to know about Objective-C to write iPhone applications. Its focus is on the right way to structure your application — what’s known as the program architecture. Having the right architecture results in an application that not only works but also can be extended to add new functionality (relatively) easily. And not only that, it enables you to easily track down and fix those pesky bugs that make their home in everyone’s apps. I also show you how to deal with the

How This All-in-One Book Is Organized

03_542934-intro.indd 403_542934-intro.indd 4 3/23/10 10:53 PM3/23/10 10:53 PM