CocoaTouch™foriPhone®OS3CocoaTouch™foriPhone®OS3JivaDeVoeCocoaTouch™foriPhone®OS3PublishedbyWileyPublishing,Inc.10475CrosspointBoulevardIndianapolis,IN46256www.wiley.comCopyright©2010byWileyPublishing,Inc.,Indianapolis,IndianaPublishedbyWileyPublishing,Inc.,Indianapolis,IndianaPublishedsimultaneouslyinCanadaISBN:978-0-470-48107-3ManufacturedintheUnitedStatesofAmerica10987654321Nopartofthispublicationmaybereproduced,storedinaretrievalsystemortransmittedinanyformorbyanymeans,electronic,mechanical,photocopying,recording,scanningorotherwise,exceptaspermittedunderSections107or108ofthe1976UnitedStatesCopyrightAct,withouteitherthepriorwrittenpermissionofthePublisher,orauthorizationthroughpaymentoftheappropriateper-copyfeetotheCopyrightClearanceCenter,222RosewoodDrive,Danvers,MA01923,(978)750-8400,fax(978)646-8600.RequeststothePublisherforpermissionshouldbeaddressedtothePermissionsDepartment,JohnWiley&Sons,Inc.,111RiverStreet,Hoboken,NJ07030,201-748-6011,fax201-748-6008,oronlineat:THEPUBLISHERANDTHEAUTHORMAKENOREPRESENTATIONSORWARRANTIESWITHRESPECTTOTHEACCURACYORCOMPLETENESSOFTHECONTENTSOFTHISWORKANDSPECIFICALLYDISCLAIMALLWARRANTIES,INCLUDINGWITHOUTLIMITATIONWARRANTIESOFFITNESSFORAPARTICULARPURPOSE.NOWARRANTYMAYBECREATEDOREXTENDEDBYSALESORPROMOTIONALMATERIALS.THEADVICEANDSTRATEGIESCONTAINEDHEREINMAYNOTBESUITABLEFOREVERYSITUATION.THISWORKISSOLDWITHTHEUNDERSTANDINGTHATTHEPUBLISHERISNOTENGAGEDINRENDERINGLEGAL,ACCOUNTING,OROTHERPROFESSIONALSERVICES.IFPROFESSIONALASSISTANCEISREQUIRED,THESERVICESOFACOMPETENTPROFESSIONALPERSONSHOULDBESOUGHT.NEITHERTHEPUBLISHERNORTHEAUTHORSHALLBELIABLEFORDAMAGESARISINGHEREFROM.THEFACTTHATANORGANIZATIONORWEBSITEISREFERREDTOINTHISWORKASACITATIONAND/ORAPOTENTIALSOURCEOFFURTHERINFORMATIONDOESNOTMEANTHATTHEAUTHORORTHEPUBLISHERENDORSESTHEINFORMATIONTHEORGANIZATIONORWEBSITEMAYPROVIDEORRECOMMENDATIONSITMAYMAKE.FURTHER,READERSSHOULDBEAWARETHATINTERNETWEBSITESLISTEDINTHISWORKMAYHAVECHANGEDORDISAPPEAREDBETWEENWHENTHISWORKWASWRITTENANDWHENITISREAD.Forgeneralinformationonourotherproductsandservicesortoobtaintechnicalsupport,pleasecontactourCustomerCareDepartmentwithintheU.S.at(877)762-2974,outsidetheU.S.at(317)572-3993orfax(317)572-4002.LibraryofCongressControlNumber:2009937274Trademarks:WileyandtheWileylogoareregisteredtrademarksofJohnWiley&Sons,Inc.and/oritsaffiliates,intheUnitedStatesandothercountries,andmaynotbeusedwithoutwrittenpermission.CocoaTouchandiPhonearetrademarksorregisteredtrademarksofApple,Inc.Allothertrademarksarethepropertyoftheirrespectiveowners.WileyPublishing,Inc.,isnotassociatedwithanyproductorvendormentionedinthisbook.CocoaTouchforiPhoneOS3isanindependentpublicationandhasnotbeenauthorized,sponsored,orotherwiseapprovedbyApple,Inc.Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsinprintmaynotbeavailableinelectronicbooks.Formywife,Dawn,andmychildren,Robert,Alex,andIzzy.Youare,andalwayswillbe,themostimportantpeopleintheworldtome.AbouttheAuthorJivaDeVoehasbeenwritingsoftwarefornearly25years,startingwithhisCommodoreVIC-20usingBASICandassemblylanguage,andgraduallyworkinghiswaythroughC,C++,Python,Ruby,Java,andfinally,Objective-C.In2001,hefoundedRandomIdeas,LLC,asoft-warecompanydedicatedtobuildinggreatapplicationsfortheMac.WhentheiPhoneSDKwasannounced,hewashonoredtobeselectedasoneoftheearliestdeveloperstohaveaccesstotheSDK,andusingit,hedevelopedseveralapplicationsthatwereavailableintheiTunesAppStorewhenitlaunchedonJuly11,2008.Sincethattime,hisiPhoneapplicationshavereceivedseveralawards—includingbeingchosenasamongtheTop100appsandgamesintheAppStore—andbeenfeaturedasAppleStaffPicksandinAppleadvertise-ments.Today,Jivacontinuestoworkfulltimeforhiscompany,developinggreatiPhoneandMacapplications.Heliveswithhiswife,threechildren,andtwobassethoundsinthedesertofArizona.CreditsAcquisitionsEditorAaronBlackExecutiveEditorJodyLefevereProjectEditorMartinV.MinnerTechnicalEditorDallasBrownCopyEditorMarylouiseWiackEditorialDirectorRobynSieskyEditorialManagerCricketKrengelBusinessManagerAmyKniesSeniorMarketingManagerSandySmithVicePresidentandExecutiveGroupPublisherRichardSwadleyVicePresidentandExecutivePublisherBarryPruettProjectCoordinatorKatieCrockerGraphicsandProductionSpecialistsAndreaHornbergerJenniferMayberryMarkPintoQualityControlTechnicianRebeccaDenoncourProofreadingChristineSabooniIndexingBIMIndexing&ProofreadingMediaDevelopmentProjectManagerLauraMossMediaDevelopmentAssistantProjectManagerJennySwisherixContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixAcknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiPartI:GettingStartedwiththeiPhone................................................1Chapter1:GettingtoKnowYourTools...................................3IntroducingXcode.............................................................