Cyan yelloW SPoT MaTTe MaGenTa BlaCk PanTone 123 C www.dbebooks.com - Free Books & magazines BookS for ProfeSSIonalS By ProfeSSIonalS® Companion • a complete, soup-to-nuts course in iphone and ipod touch AeBvBaiolaoblke A complete course in iPhone foarn SddU rkepv d3iasetedd and iPod touch programming programming, written in an easy-to-read style and perfect for e g beginners and advanced readers alike i • n packed full of intricate, reusable examples that showcase the n nearly endless possibilities of the iphone Sdk i n • Updated edition fully compatible with apple’s iphone Sdk 3 g a i re you a programmer looking for a new challenge? Does the thought of P building your very own iPhone application make your heart race and your pulse quicken? If so, then Beginning iPhone 3 Development is the book for you. h assuming a minimal working knowledge of objective-C, this book starts with o the basics, walking you through the process of downloading and installing apple’s free iPhone SDk 3 and stepping you through the creation of your first n simple iPhone application. you’ll move on from there, mastering the iPhone interface elements that you’ve come to know and love, such as buttons, e switches, pickers, toolbars, and sliders. you’ll master tables and learn how to save your data using the iPhone file system, 3 as well as using SQlite, iPhone’s built-in database management system. you’ll also draw using Quartz 2D and openGl eS; add gestural support to your applications; D work with the camera, the photo library, the accelerometer, preferences, local- ization, and Core location; and do much more. e apple’s iPhone SDk 3, this book, and your imagination are all you’ll need to v start building your very own best-selling iPhone applications. e Beginning Dave Mark is a longtime mac developer and author of a number of bestselling books including Learn C on the Mac, the Macintosh l Programming Primer series, and Ultimate Mac Programming. o Jeff LaMarche is a seasoned mac developer. He’s written about Cocoa p iPhone 3 Development and objective-C for MacTech Magazine and has written articles for m apple’s developer technical Services web site. e n Exploring the iPhone SDK S e tl t ti d e t a l e L r a M Dave Mark | Jeff LaMarche aM r Companion eBook See laSt page for detailS on $10 eBook verSion ISBN 978-1-4302-2459-4 char ek 53999 US $39.99 Shelve in Mobile Computing/Macintosh Programming SOURCE CODE ONLINE User level: www.apress.com Beginner–Intermediate 9 781430 224594 www.iphonedevbook.com this print for content only—size & color not accurate spine = 1.114" 584 page count Download at Boykma.Com Beginning iPhone 3 Development Exploring the iPhone SDK Dave Mark Jeff LaMarche Download at Boykma.Com 24594FM.indd 1 6/25/09 2:18:04 PM Beginning iPhone 3 Development: Exploring the iPhone SDK Copyright © 2009 by Dave Mark and Jeff LaMarche All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2459-4 ISBN-13 (electronic): 978-1-4302-2460-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editors: Clay Andres, Douglas Pundick Technical Reviewer: Mark Dalrymple Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Managers: Grace Wong, Beth Christmas Copy Editors: Kim Wimpsett, Heather Lang Associate Production Director: Kari Brooks-Copony Senior Production Editor: Laura Cheu Compositor: Dina Quan Proofreader: Nancy Sixsmith Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales– eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. Download at Boykma.Com 24594FM.indd 2 6/25/09 2:18:04 PM To Deneen, you are the light of my life. LFU4FREIH. . . —Dave To the most important people in my life, my wife and kids. —Jeff Download at Boykma.Com 24594FM.indd 3 6/25/09 2:18:04 PM Download at Boykma.Com 24594FM.indd 4 6/25/09 2:18:05 PM Contents at a Glance About the Author .............................................................xvii About the Technical Reviewer ................................................. xix Acknowledgments ............................................................ xxi Preface to Beginning iPhone 3 Development ...................................xxiii Preface to Beginning iPhone 2 Development ....................................xxv chapter 1 Welcome to the Jungle ...................................... 1 chapter 2 Appeasing the Tiki Gods ....................................13 chapter 3 Handling Basic Interaction ..................................31 chapter 4 More User Interface Fun ....................................57 chapter 5 Autorotation and Autosizing ................................99 chapter 6 Multiview Applications ....................................119 chapter 7 Tab Bars and Pickers .......................................145 chapter 8 Introduction to Table Views ................................193 chapter 9 Navigation Controllers and Table Views .....................247 chapter 10 Application Settings and User Defaults .....................321 chapter 11 Basic Data Persistence .....................................347 chapter 12 Drawing with Quartz and OpenGL .........................397 chapter 13 Taps, Touches, and Gestures ...............................437 chapter 14 Where Am I? Finding Your Way with Core Location ..........465 chapter 15 Whee! Accelerometer! .....................................477 chapter 16 iPhone Camera and Photo Library ..........................499 chapter 17 Application Localization ...................................511 chapter 18 Where to Next? ...........................................531 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Download at Boykma.Com v 24594FM.indd 5 6/25/09 2:18:05 PM Download at Boykma.Com 24594FM.indd 6 6/25/09 2:18:05 PM Contents About the Author .............................................................xvii About the Technical Reviewer ................................................. xix Acknowledgments ............................................................ xxi Preface to Beginning iPhone 3 Development ...................................xxiii Preface to Beginning iPhone 2 Development ....................................xxv chapter 1 Welcome to the Jungle . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What This Book Is ..............................................2 What You Need Before You Can Begin ...........................2 What You Need to Know Before You Begin ......................5 What’s Different About Coding for iPhone? .....................6 Only One Running Application ............................6 Only One Window .........................................6 Limited Access ............................................6 Limited Response Time ....................................7 Limited Screen Size .......................................7 Limited System Resources .................................7 No Garbage Collection ....................................8 Some New Stuff ...........................................8 A Different Approach ......................................8 What’s in This Book ............................................8 Chapter 2 .................................................8 Chapter 3 .................................................8 Chapter 4 .................................................9 Chapter 5 .................................................9 Chapter 6 .................................................9 Chapter 7 .................................................9 Chapter 8 .................................................9 Chapter 9 .................................................9 Chapter 10 ................................................9 Chapter 11 ................................................9 Chapter 12 ...............................................10 Chapter 13 ...............................................10 Chapter 14 ...............................................10 Download at Boykma.Com vii 24594FM.indd 7 6/25/09 2:18:05 PM viii CONTENTS Chapter 15 ...............................................10 Chapter 16 ...............................................10 Chapter 17 ...............................................10 Chapter 18 ...............................................10 What’s New in This Update? ...................................11 Are You Ready? ...............................................11 chapter 2 Appeasing the Tiki Gods . . . . . . . . . . . . . . . . . . . . . . . 13 Setting Up Your Project in Xcode ..............................13 The Xcode Project Window ...............................16 Introducing Interface Builder ..................................19 What’s in the Nib File? ....................................21 Adding a Label to the View ...............................22 Some iPhone Polish—Finishing Touches .......................25 Ready to Compile and Run ...............................28 Bring It on Home .............................................29 chapter 3 Handling Basic Interaction . . . . . . . . . . . . . . . . . . . . . 31 The Model-View-Controller Paradigm ..........................31 Creating Our Project ..........................................33 Creating the View Controller ..................................33 Outlets ..................................................34 Actions ..................................................36 Adding Actions and Outlets to the View Controller ........36 Adding Actions and Outlets to the Implementation File ...39 Using the Application Delegate ...............................44 Editing MainWindow.xib ......................................47 Editing Button_FunViewController.xib .........................49 Creating the View in Interface Builder .....................49 Connecting Everything ..................................52 Trying It Out .............................................56 Bring It on Home .............................................56 chapter 4 More User Interface Fun . . . . . . . . . . . . . . . . . . . . . . . 57 A Screen Full of Controls ......................................58 Active, Static, and Passive Controls ............................59 Creating the Application ......................................60 Importing the Image .....................................60 Implementing the Image View and Text Fields. . . . . . . . . . . . .61 Download at Boykma.Com 24594FM.indd 8 6/25/09 2:18:05 PM