ebook img

More iPhone 3 Development: Tackling iPhone SDK 3 (Beginning) PDF

577 Pages·2009·9.72 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview More iPhone 3 Development: Tackling iPhone SDK 3 (Beginning)

CyAn yelloW SpoT MATTe MAGenTA BlACk pAnTone 123 C eS I BookS for profeSSIonAlS By profeSSIonAlS® ACeovBmapiolaanobiolken More Great iPhone API iphone d BByee thvgee bienlsotn-seillningg au thors of tl nterested in iphone development? Want to learn more? Whether you’re a M p ated ti Besgeilnf ntainugg ihPth oipnheo 3n De edveevlo pomr heanvt,e w jue’svte m gaodt ea gyoreuart wneawy tbhoroouk gfohr tyhoeu .pages of o Coverage, Depth, and Insight ment l re More iPhone 3 Development: Tackling iPhone SDK 3 digs deep into Apple’s latest r SDk, with bestselling authors Dave Mark and Jeff laMarche explaining things e as only they can, covering topics like Core data, peer-to-peer networking us- ing gamekit and network streams, working with data from the web, mapkit, i in-application e-mail, and more. All the concepts and ApIs are clearly laid out P and come complete with code snippets you can customize for use in your most ambitious apps. h More iPhone 3 Development continues right where Beginning iPhone 3 Develop- o ment left off, starting with a series of chapters devoted to Core Data, Apple’s n new standard for iphone persistence. Jeff and Dave step you through the key concepts of Core Data, including techniques and tips specifically for writing e large-Scale applications. for writing professional iphone apps, you’ll want to embrace Core Data,. 3 The depth and breadth of Core Data coverage alone is worth the price of ad- D mission, but there’s so much more. This book covers a variety of networking mechanisms, from Gamekit’s relatively simple Bluetooth peer-to-peer model, e to the addition of Bonjour discovery and network streams, through the com- plexity of acquiring information through Web file access. Dave and Jeff also v take you through advanced topics, such as Concurrent programming and e techniques for debugging. l your knowledge of iphone app creation can’t be considered complete until o you’ve mastered all the knowledge imparted and techniques revealed in More p More iPhone 3 Development. m Dave Mark is a long-time Mac developer and author and has written a num- ber of books on Macintosh development, including Learn C on the Macintosh, The Macintosh Programming Primer series, and Ultimate Mac Programming. His e blog can be found at www.davemark.com. n iPhone 3 Development Jeff LaMarche is a longtime mac developer, and apple iphone t developer. With over 20 years of programming experience, he’s written on Cocoa and objective-C for mactech magazine, as well as articles for apple’s developer technical Services website. He has experience working in enterprise software, both as a developer for peopleSoft Tackling iPhone SDK 3 starting in the late 1990s, and then later as an independent consultant. M Dave Mark  |  Jeff LaMarche a Companion eBook See laSt page for detailS on $10 eBook verSion ISBN 978-1-4302-2505-8 rk 53999 US $39.99 Shelve in: Mobile Computing/Mac Development SOURCE CODE ONLINE User level: www.apress.com Intermediate–Advanced 9 781430 225058 this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 0.000" 000 page count CyAn yelloW SpoT MATTe MAGenTA BlACk pAnTone 123 C eS I BookS for profeSSIonAlS By profeSSIonAlS® ACeovBmapiolaanobiolken More Great iPhone API iphone d BByee thvgee bienlsotn-seillningg au thors of tl nterested in iphone development? Want to learn more? Whether you’re a M p ated ti Besgeilnf ntainugg ihPth oipnheo 3n De edveevlo pomr heanvt,e w jue’svte m gaodt ea gyoreuart wneawy tbhoroouk gfohr tyhoeu .pages of o Coverage, Depth, and Insight ment l re More iPhone 3 Development: Tackling iPhone SDK 3 digs deep into Apple’s latest r SDk, with bestselling authors Dave Mark and Jeff laMarche explaining things e as only they can, covering topics like Core data, peer-to-peer networking us- ing gamekit and network streams, working with data from the web, mapkit, i in-application e-mail, and more. All the concepts and ApIs are clearly laid out P and come complete with code snippets you can customize for use in your most ambitious apps. h More iPhone 3 Development continues right where Beginning iPhone 3 Develop- o ment left off, starting with a series of chapters devoted to Core Data, Apple’s n new standard for iphone persistence. Jeff and Dave step you through the key concepts of Core Data, including techniques and tips specifically for writing e large-Scale applications. for writing professional iphone apps, you’ll want to embrace Core Data,. 3 The depth and breadth of Core Data coverage alone is worth the price of ad- D mission, but there’s so much more. This book covers a variety of networking mechanisms, from Gamekit’s relatively simple Bluetooth peer-to-peer model, e to the addition of Bonjour discovery and network streams, through the com- plexity of acquiring information through Web file access. Dave and Jeff also v take you through advanced topics, such as Concurrent programming and e techniques for debugging. l your knowledge of iphone app creation can’t be considered complete until o you’ve mastered all the knowledge imparted and techniques revealed in More p More iPhone 3 Development. m Dave Mark is a long-time Mac developer and author and has written a num- ber of books on Macintosh development, including Learn C on the Macintosh, The Macintosh Programming Primer series, and Ultimate Mac Programming. His e blog can be found at www.davemark.com. n iPhone 3 Development Jeff LaMarche is a longtime mac developer, and apple iphone t developer. With over 20 years of programming experience, he’s written on Cocoa and objective-C for mactech magazine, as well as articles for apple’s developer technical Services website. He has experience working in enterprise software, both as a developer for peopleSoft Tackling iPhone SDK 3 starting in the late 1990s, and then later as an independent consultant. M Dave Mark  |  Jeff LaMarche a Companion eBook See laSt page for detailS on $10 eBook verSion ISBN 978-1-4302-2505-8 rk 53999 US $39.99 Shelve in: Mobile Computing/Mac Development SOURCE CODE ONLINE User level: www.apress.com Intermediate–Advanced 9 781430 225058 this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 0.000" 000 page count More iPhone 3 Development: iPhone Tackling SDK 3 ■ ■ ■ Dave Mark Jeff LaMarche i More iPhone 3 Development: Tackling iPhone SDK 3 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-2505-8 ISBN-13 (electronic): 978-1-4302-2743-4 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. President and Publisher: Paul Manning Lead Editor: Clay Andres Developmental Editor: Douglas Pundick Technical Reviewer: Mark Dalrymple Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Kelly Moritz Copy Editor: Marilyn Smith and Ralph Moore Compositor: MacPS, LLC Indexers: John Collin and Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko 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 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. You will need to answer questions pertaining to this book in order to successfully download the code. ii To Deneen, Daniel, Kelley, and Ryan, LFU4FRNMWWA. . . —Dave To the most important people in my life, my wife and kids. —Jeff Contents at a Glance ■Contents at a Glance..............................................................................................................iv ■Contents..................................................................................................................................v ■About the Authors.................................................................................................................xii ■About the Technical Reviewer.............................................................................................xiii ■Acknowlegments.................................................................................................................xiv ■Preface..................................................................................................................................xv ■Chapter 1: Here We Go Round Again.......................................................................................1(cid:1) Part I: Core Data.........................................................................................................................7(cid:1) ■Chapter 2: The Anatomy of Core Data.....................................................................................9(cid:1) ■Chapter 3: A Super Start: Adding, Displaying, and Deleting Data.........................................41(cid:1) ■Chapter 4: The Devil in the Detail View.................................................................................83(cid:1) ■Chapter 5: Preparing for Change: Migrations and Versioning............................................127(cid:1) ■Chapter 6: Custom Managed Objects..................................................................................137(cid:1) ■Chapter 7: Relationships, Fetched Properties, and Expressions........................................169(cid:1) Part II: Further Explorations...................................................................................................223(cid:1) ■Chapter 8: Peer-to-Peer Over Bluetooth Using GameKit.....................................................225(cid:1) ■Chapter 9: Online Play: Bonjour and Network Streams......................................................271(cid:1) ■Chapter 10: Working with Data from the Web....................................................................329(cid:1) ■Chapter 11: MapKit.............................................................................................................359(cid:1) ■Chapter 12: Sending Mail....................................................................................................391(cid:1) ■Chapter 13: iPod Library Access.........................................................................................405(cid:1) ■Chapter 14: Keeping Your Interface Responsive.................................................................451(cid:1) ■Chapter 15: Debugging.......................................................................................................495(cid:1) ■Chapter 16: The Road Goes Ever On…................................................................................527 ■Index...................................................................................................................................531 iv Contents ■Contents at a Glance..............................................................................................................iv(cid:1) ■Contents..................................................................................................................................v(cid:1) ■About the Authors.................................................................................................................xii(cid:1) ■About the Technical Reviewer.............................................................................................xiii(cid:1) ■Acknowledgments...............................................................................................................xiv(cid:1) ■Preface..................................................................................................................................xv(cid:1) ■Chapter 1: Here We Go Round Again.......................................................................................1(cid:1) What This Book Is....................................................................................................................................................................................1(cid:1) What You Need to Know..........................................................................................................................................................................1(cid:1) What You Need Before You Can Begin....................................................................................................................................................2(cid:1) What’s In this Book..................................................................................................................................................................................4(cid:1) Are You Ready?.......................................................................................................................................................................................6(cid:1) Part I: Core Data.........................................................................................................................7(cid:1) ■Chapter 2: The Anatomy of Core Data.....................................................................................9(cid:1) A Brief History of Core Data...................................................................................................................................................................10(cid:1) Creating a Core Data Template Application...........................................................................................................................................10(cid:1) Core Data Concepts and Terminology...................................................................................................................................................12(cid:1) The Data Model and Persistent Store....................................................................................................................................................13(cid:1) The Data Model Class: NSManagedObjectModel..............................................................................................................................14(cid:1) The Persistent Store and the Persistent Store Coordinator..............................................................................................................16(cid:1) Reviewing the Data Model................................................................................................................................................................18(cid:1) Entities and the Data Model Editor........................................................................................................................................................18(cid:1) Entities..............................................................................................................................................................................................18(cid:1) Properties.........................................................................................................................................................................................19(cid:1) Managed Objects...................................................................................................................................................................................21(cid:1) Key-Value Coding.............................................................................................................................................................................21(cid:1) Managed Object Context..................................................................................................................................................................22(cid:1) Saves on Terminate..........................................................................................................................................................................23 v ■ CONTENTS Loading Data from the Persistent Store................................................................................................................................................24(cid:1) The Fetched Results Controller.............................................................................................................................................................26(cid:1) Creating a Fetched Results Controller..............................................................................................................................................26(cid:1) The Fetched Results Controller Delegate Methods...........................................................................................................................29(cid:1) Retrieving a Managed Object from the Fetched Results Controller..................................................................................................36(cid:1) Creating and Inserting a New Managed Object................................................................................................................................36(cid:1) Deleting Managed Objects................................................................................................................................................................38(cid:1) Putting Everything in Context................................................................................................................................................................39(cid:1) ■Chapter 3: A Super Start: Adding, Displaying, and Deleting Data.........................................41(cid:1) Setting up the Xcode Project.................................................................................................................................................................42(cid:1) Application Architecture...................................................................................................................................................................43(cid:1) Modifying the Application Delegate Interface...................................................................................................................................44(cid:1) Adding to the Application Delegate Implementation........................................................................................................................45(cid:1) Creating the Table View Controller........................................................................................................................................................46(cid:1) Setting up MainWindow.xib...................................................................................................................................................................47(cid:1) Connecting the Outlets.....................................................................................................................................................................50(cid:1) Designing the Data Model.....................................................................................................................................................................50(cid:1) Adding an Entity...............................................................................................................................................................................51(cid:1) Editing the New Entity......................................................................................................................................................................52(cid:1) Adding Attributes to the New Entity.................................................................................................................................................54(cid:1) Adding the Name Attribute...............................................................................................................................................................54(cid:1) Editing the Attribute..........................................................................................................................................................................55(cid:1) Creating HeroListViewController............................................................................................................................................................60(cid:1) Declaring the Fetched Results Controller.........................................................................................................................................60(cid:1) Drag Two Icons to Your Project........................................................................................................................................................63(cid:1) Designing the HeroListViewController Interface...............................................................................................................................64(cid:1) Implementing the Hero View Controller............................................................................................................................................66(cid:1) Let ‘Er Rip..............................................................................................................................................................................................79(cid:1) Done, but Not Done...............................................................................................................................................................................81(cid:1) ■Chapter 4: The Devil in the Detail View.................................................................................83(cid:1) Table-Based vs. Nib-Based Detail Views...............................................................................................................................................84(cid:1) Detail Editing View Challenges..............................................................................................................................................................85(cid:1) Controlling Table Structure with Arrays................................................................................................................................................87(cid:1) Paired Arrays.........................................................................................................................................................................................87(cid:1) Nested Arrays...................................................................................................................................................................................88(cid:1) Paired Nested Arrays........................................................................................................................................................................89(cid:1) Representing Our Table Structure with Arrays.................................................................................................................................89(cid:1) Nested Arrays, Categorically Speaking............................................................................................................................................90(cid:1) Updating the SuperDB Project..........................................................................................................................................................90(cid:1) Formatting of Attributes........................................................................................................................................................................92(cid:1) Creating the Detail View Controller.......................................................................................................................................................94(cid:1) Declaring Instance Variables and Properties....................................................................................................................................96(cid:1) Implementing the Viewing Functionality..........................................................................................................................................97(cid:1) Using the New Controller................................................................................................................................................................103(cid:1) Trying Out the View Functionality...................................................................................................................................................106(cid:1) Adding Editing Subcontrollers.............................................................................................................................................................107(cid:1) Creating the Superclass.................................................................................................................................................................108(cid:1) Creating the String Attribute Editor................................................................................................................................................112(cid:1) Creating the Date Attribute Editor...................................................................................................................................................115(cid:1) Using the Attribute Editors..............................................................................................................................................................118(cid:1) vi ■ CONTENTS Implementing a Selection List.............................................................................................................................................................120(cid:1) Creating the Generic Selection List Controller................................................................................................................................120(cid:1) Devil’s End...........................................................................................................................................................................................125(cid:1) ■Chapter 5: Preparing for Change: Migrations and Versioning............................................127(cid:1) About Data Models..............................................................................................................................................................................128(cid:1) Data Models Are Compiled.............................................................................................................................................................128(cid:1) Data Models Can Have Multiple Versions.......................................................................................................................................129(cid:1) Data Model Version Identifiers.......................................................................................................................................................131(cid:1) Using the Versioned Data Model.....................................................................................................................................................132(cid:1) Migrations............................................................................................................................................................................................133(cid:1) Lightweight vs. Standard................................................................................................................................................................134(cid:1) Standard Migrations.......................................................................................................................................................................134(cid:1) Setting up Your App to Use Lightweight Migrations.......................................................................................................................134(cid:1) Time to Migrate On..............................................................................................................................................................................136(cid:1) ■Chapter 6: Custom Managed Objects..................................................................................137(cid:1) Updating the Data Model.....................................................................................................................................................................140(cid:1) Adding the Age Attribute................................................................................................................................................................141(cid:1) Adding the Favorite Color Attribute................................................................................................................................................141(cid:1) Adding a Minimum Length to the Name Attribute..........................................................................................................................142(cid:1) Creating the Hero Class.......................................................................................................................................................................143(cid:1) Tweaking the Hero Header..................................................................................................................................................................145(cid:1) Defaulting............................................................................................................................................................................................146(cid:1) Validation.............................................................................................................................................................................................147(cid:1) Single-Attribute Validations............................................................................................................................................................148(cid:1) Multiple-Attribute Validations.........................................................................................................................................................150(cid:1) Virtual Accessors.................................................................................................................................................................................151(cid:1) Adding Validation Feedback................................................................................................................................................................152(cid:1) Updating the ManagedObjectAttributeEditor Header File...............................................................................................................152(cid:1) Updating the ManagedObjectAttributeEditor Implementation File.................................................................................................153(cid:1) Updating the Subclasses to Use Validation....................................................................................................................................154(cid:1) Creating the Value Transformer...........................................................................................................................................................155(cid:1) Creating the Color Attribute Editor.......................................................................................................................................................158(cid:1) Displaying the New Attributes in Hero Edit Controller.........................................................................................................................161(cid:1) The Display Problem............................................................................................................................................................................163(cid:1) Adding View-Only Support to Hero Edit Controller..............................................................................................................................165(cid:1) Hiding the Disclosure Indicator......................................................................................................................................................165(cid:1) Handling Taps on Read-Only Attributes..........................................................................................................................................166(cid:1) Color Us Gone......................................................................................................................................................................................167(cid:1) ■Chapter 7: Relationships, Fetched Properties, and Expressions........................................169(cid:1) Expanding Our Application: Superpowers and Reports.......................................................................................................................170(cid:1) Relationships.......................................................................................................................................................................................172(cid:1) To-One Relationships.....................................................................................................................................................................173(cid:1) To-Many Relationships...................................................................................................................................................................174(cid:1) Inverse Relationships.....................................................................................................................................................................176(cid:1) Delete Rules...................................................................................................................................................................................177(cid:1) Fetched Properties..............................................................................................................................................................................178(cid:1) Creating Relationships and Fetched Properties in the Data Model Editor...........................................................................................179(cid:1) Adding the Power Entity.................................................................................................................................................................179(cid:1) Creating the Powers Relationship..................................................................................................................................................180(cid:1) vii ■ CONTENTS Creating the Inverse Relationship...................................................................................................................................................181(cid:1) Creating the olderHeroes Fetched Property...................................................................................................................................181(cid:1) Creating the youngerHeroes Fetched Property...............................................................................................................................185(cid:1) Creating the sameSexHeroes Fetched Property.............................................................................................................................186(cid:1) Creating the oppositeSexHeroes Fetched Property........................................................................................................................187(cid:1) Adding Relationships and Fetched Properties to the Hero Class.........................................................................................................189(cid:1) The Big Refactor..................................................................................................................................................................................190(cid:1) Renaming the Class........................................................................................................................................................................191(cid:1) Refactoring the hero Instance Variable..........................................................................................................................................193(cid:1) Removing the Arrays......................................................................................................................................................................193(cid:1) Supporting Save and Cancel Buttons.............................................................................................................................................193(cid:1) Adding Support for To-Many Relationships....................................................................................................................................197(cid:1) Using the New Generic Controller........................................................................................................................................................211(cid:1) Adding Factory Methods for Hero and Power.................................................................................................................................211(cid:1) Deleting the Nib Instance...............................................................................................................................................................215(cid:1) Updating HeroListController...........................................................................................................................................................215(cid:1) Creating the Fetched Property Attribute Controller.............................................................................................................................216(cid:1) Cleaning Up Deleted Objects...............................................................................................................................................................218(cid:1) Wonderful to the Core..........................................................................................................................................................................221(cid:1) Part II: Further Explorations...................................................................................................223(cid:1) ■Chapter 8: Peer-to-Peer Over Bluetooth Using GameKit.....................................................225(cid:1) This Chapter’s Application...................................................................................................................................................................226(cid:1) Network Communication Models........................................................................................................................................................229(cid:1) Client-Server Model........................................................................................................................................................................229(cid:1) Peer-to-Peer Model........................................................................................................................................................................230(cid:1) Hybrid Client-Server/Peer-to-Peer.................................................................................................................................................231(cid:1) The GameKit Session...........................................................................................................................................................................232(cid:1) Creating the Session......................................................................................................................................................................232(cid:1) Finding and Connecting to Other Sessions.....................................................................................................................................233(cid:1) Listening for Other Sessions...........................................................................................................................................................234(cid:1) Sending Data to a Peer...................................................................................................................................................................234(cid:1) Packaging Up Information to Send.................................................................................................................................................235(cid:1) Receiving Data from a Peer............................................................................................................................................................236(cid:1) Closing Connections.......................................................................................................................................................................237(cid:1) The Peer Picker...................................................................................................................................................................................237(cid:1) Creating the Peer Picker.................................................................................................................................................................237(cid:1) Handling a Peer Connection...........................................................................................................................................................238(cid:1) Creating the Session......................................................................................................................................................................238(cid:1) Creating the Project.............................................................................................................................................................................239(cid:1) Turning Off the Idle Timer...............................................................................................................................................................239(cid:1) Importing the GameKit Framework................................................................................................................................................240(cid:1) Designing the Interface..................................................................................................................................................................241(cid:1) Trying It Out.........................................................................................................................................................................................268(cid:1) Game On!.............................................................................................................................................................................................269(cid:1) ■Chapter 9: Online Play: Bonjour and Network Streams......................................................271(cid:1) This Chapter’s Application...................................................................................................................................................................271(cid:1) Overview of the Process......................................................................................................................................................................273(cid:1) Setting Up a Listener...........................................................................................................................................................................273(cid:1) Callback Functions and Run Loop Integration................................................................................................................................274(cid:1) viii

Description:
I have been searching for a quality iPhone Development book for beginners. There are lots of books on iPhone development available, but it is VERY difficult to find something for real beginners. Obective-C is not HTML and it takes real effort to understand iPhone development. To that end, this book
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.