COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION US $39.99 Shelve in: Mobile Computing/Mac Development User level: Intermediate–Advanced www.apress.com SOURCE CODE ONLINE BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® this print for content only—size & color not accurate CYAN MAGENTA YELLOW BLACK PANTONE 123 C SPOT MATTE ISBN 978-1-4302-2505-8 9 7814 30 225058 5 39 9 9 I nterested in iPhone development? Want to learn more? Whether you’re a self taught iPhone dev or have just made your way through the pages of Beginning iPhone 3 Development, we’ve got a great new book for you. More iPhone 3 Development: Tackling iPhone SDK 3 digs deep into Apple’s latest SDK, with bestselling authors Dave Mark and Je LaMarche explaining things 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, in-application e-mail, and more. All the concepts and APIs are clearly laid out and come complete with code snippets you can customize for use in your most ambitious apps. More iPhone 3 Development continues right where Beginning iPhone 3 Develop- ment left o, starting with a series of chapters devoted to Core Data, Apple’s new standard for iPhone Persistence. Je and Dave step you through the key concepts of Core Data, including techniques and tips specically for writing Large-Scale Applications. For writing professional iPhone apps, you’ll want to embrace Core Data,. The depth and breadth of Core Data coverage alone is worth the price of ad- 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, to the addition of Bonjour discovery and network streams, through the com- plexity of acquiring information through Web File Access. Dave and Je also take you through advanced topics, such as Concurrent Programming and techniques for Debugging. Your knowledge of iPhone app creation can’t be considered complete until you’ve mastered all the knowledge imparted and techniques revealed in More iPhone 3 Development. 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 blog can be found at www.davemark.com. Je LaMarche is a longtime Mac developer, and Apple iPhone 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 starting in the late 1990s, and then later as an independent consultant. Trim: 7.5 x 9.25 spine = 0.000" 000 page count Mark More iPhone 3 Development Companion eBook Available More Great iPhone API Coverage, Depth, and Insight More iPhone 3 Development Tackling iPhone SDK 3 Dave Mark | Jeff LaMarche RELATED TITLES By the best-selling authors of Beginning iPhone Development i More iPhone 3 Development: Tackling iPhone SDK 3 ■ ■ ■ Dave Mark Jeff LaMarche ii 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 orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please e-mail info@apress.com, 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. iii To Deneen, Daniel, Kelley, and Ryan, LFU4FRNMWWA. . . —Dave To the most important people in my life, my wife and kids. —Jeff iv 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 Part I: Core Data 7 ■Chapter 2: The Anatomy of Core Data 9 ■Chapter 3: A Super Start: Adding, Displaying, and Deleting Data 41 ■Chapter 4: The Devil in the Detail View 83 ■Chapter 5: Preparing for Change: Migrations and Versioning 127 ■Chapter 6: Custom Managed Objects 137 ■Chapter 7: Relationships, Fetched Properties, and Expressions 169 Part II: Further Explorations 223 ■Chapter 8: Peer-to-Peer Over Bluetooth Using GameKit 225 ■Chapter 9: Online Play: Bonjour and Network Streams 271 ■Chapter 10: Working with Data from the Web 329 ■Chapter 11: MapKit 359 ■Chapter 12: Sending Mail 391 ■Chapter 13: iPod Library Access 405 ■Chapter 14: Keeping Your Interface Responsive 451 ■Chapter 15: Debugging 495 ■Chapter 16: The Road Goes Ever On… 527 ■Index 531 v Contents ■Contents at a Glance iv ■Contents v ■About the Authors xii ■About the Technical Reviewer xiii ■Acknowledgments xiv ■Preface xv ■Chapter 1: Here We Go Round Again 1 What This Book Is 1 What You Need to Know 1 What You Need Before You Can Begin 2 What’s In this Book 4 Are You Ready? 6 Part I: Core Data 7 ■Chapter 2: The Anatomy of Core Data 9 A Brief History of Core Data 10 Creating a Core Data Template Application 10 Core Data Concepts and Terminology 12 The Data Model and Persistent Store 13 The Data Model Class: NSManagedObjectModel 14 The Persistent Store and the Persistent Store Coordinator 16 Reviewing the Data Model 18 Entities and the Data Model Editor 18 Entities 18 Properties 19 Managed Objects 21 Key-Value Coding 21 Managed Object Context 22 Saves on Terminate 23 ■ CONTENTS vi Loading Data from the Persistent Store 24 The Fetched Results Controller 26 Creating a Fetched Results Controller 26 The Fetched Results Controller Delegate Methods 29 Retrieving a Managed Object from the Fetched Results Controller 36 Creating and Inserting a New Managed Object 36 Deleting Managed Objects 38 Putting Everything in Context 39 ■Chapter 3: A Super Start: Adding, Displaying, and Deleting Data 41 Setting up the Xcode Project 42 Application Architecture 43 Modifying the Application Delegate Interface 44 Adding to the Application Delegate Implementation 45 Creating the Table View Controller 46 Setting up MainWindow.xib 47 Connecting the Outlets 50 Designing the Data Model 50 Adding an Entity 51 Editing the New Entity 52 Adding Attributes to the New Entity 54 Adding the Name Attribute 54 Editing the Attribute 55 Creating HeroListViewController 60 Declaring the Fetched Results Controller 60 Drag Two Icons to Your Project 63 Designing the HeroListViewController Interface 64 Implementing the Hero View Controller 66 Let ‘Er Rip 79 Done, but Not Done 81 ■Chapter 4: The Devil in the Detail View 83 Table-Based vs. Nib-Based Detail Views 84 Detail Editing View Challenges 85 Controlling Table Structure with Arrays 87 Paired Arrays 87 Nested Arrays 88 Paired Nested Arrays 89 Representing Our Table Structure with Arrays 89 Nested Arrays, Categorically Speaking 90 Updating the SuperDB Project 90 Formatting of Attributes 92 Creating the Detail View Controller 94 Declaring Instance Variables and Properties 96 Implementing the Viewing Functionality 97 Using the New Controller 103 Trying Out the View Functionality 106 Adding Editing Subcontrollers 107 Creating the Superclass 108 Creating the String Attribute Editor 112 Creating the Date Attribute Editor 115 Using the Attribute Editors 118 ■ CONTENTS vii Implementing a Selection List 120 Creating the Generic Selection List Controller 120 Devil’s End 125 ■Chapter 5: Preparing for Change: Migrations and Versioning 127 About Data Models 128 Data Models Are Compiled 128 Data Models Can Have Multiple Versions 129 Data Model Version Identifiers 131 Using the Versioned Data Model 132 Migrations 133 Lightweight vs. Standard 134 Standard Migrations 134 Setting up Your App to Use Lightweight Migrations 134 Time to Migrate On 136 ■Chapter 6: Custom Managed Objects 137 Updating the Data Model 140 Adding the Age Attribute 141 Adding the Favorite Color Attribute 141 Adding a Minimum Length to the Name Attribute 142 Creating the Hero Class 143 Tweaking the Hero Header 145 Defaulting 146 Validation 147 Single-Attribute Validations 148 Multiple-Attribute Validations 150 Virtual Accessors 151 Adding Validation Feedback 152 Updating the ManagedObjectAttributeEditor Header File 152 Updating the ManagedObjectAttributeEditor Implementation File 153 Updating the Subclasses to Use Validation 154 Creating the Value Transformer 155 Creating the Color Attribute Editor 158 Displaying the New Attributes in Hero Edit Controller 161 The Display Problem 163 Adding View-Only Support to Hero Edit Controller 165 Hiding the Disclosure Indicator 165 Handling Taps on Read-Only Attributes 166 Color Us Gone 167 ■Chapter 7: Relationships, Fetched Properties, and Expressions 169 Expanding Our Application: Superpowers and Reports 170 Relationships 172 To-One Relationships 173 To-Many Relationships 174 Inverse Relationships 176 Delete Rules 177 Fetched Properties 178 Creating Relationships and Fetched Properties in the Data Model Editor 179 Adding the Power Entity 179 Creating the Powers Relationship 180 ■ CONTENTS viii Creating the Inverse Relationship 181 Creating the olderHeroes Fetched Property 181 Creating the youngerHeroes Fetched Property 185 Creating the sameSexHeroes Fetched Property 186 Creating the oppositeSexHeroes Fetched Property 187 Adding Relationships and Fetched Properties to the Hero Class 189 The Big Refactor 190 Renaming the Class 191 Refactoring the hero Instance Variable 193 Removing the Arrays 193 Supporting Save and Cancel Buttons 193 Adding Support for To-Many Relationships 197 Using the New Generic Controller 211 Adding Factory Methods for Hero and Power 211 Deleting the Nib Instance 215 Updating HeroListController 215 Creating the Fetched Property Attribute Controller 216 Cleaning Up Deleted Objects 218 Wonderful to the Core 221 Part II: Further Explorations 223 ■Chapter 8: Peer-to-Peer Over Bluetooth Using GameKit 225 This Chapter’s Application 226 Network Communication Models 229 Client-Server Model 229 Peer-to-Peer Model 230 Hybrid Client-Server/Peer-to-Peer 231 The GameKit Session 232 Creating the Session 232 Finding and Connecting to Other Sessions 233 Listening for Other Sessions 234 Sending Data to a Peer 234 Packaging Up Information to Send 235 Receiving Data from a Peer 236 Closing Connections 237 The Peer Picker 237 Creating the Peer Picker 237 Handling a Peer Connection 238 Creating the Session 238 Creating the Project 239 Turning Off the Idle Timer 239 Importing the GameKit Framework 240 Designing the Interface 241 Trying It Out 268 Game On! 269 ■Chapter 9: Online Play: Bonjour and Network Streams 271 This Chapter’s Application 271 Overview of the Process 273 Setting Up a Listener 273 Callback Functions and Run Loop Integration 274 [...]... 32 8 ■Chapter 10 : Working with Data from the Web 32 9 Setting Up the Application Skeleton 33 1 Declaring Actions and Outlets 33 1 Designing the Interface 33 3 Implementing the Stubs 33 5 Retrieving Data Using Foundation Objects 33 6 Retrieving Data Synchronously 33 9 The URL... 35 8 ■Chapter 11 : MapKit .35 9 This Chapter’s Application 36 0 Overview and Terminology 36 1 The Map View 36 2 Map Types 36 2 User Location 36 4 Coordinate Regions 36 4 Setting the Region to Display 36 7 The Map View Delegate 36 7... 37 5 Building the Interface 37 6 Writing the Annotation Object Class 37 8 Implementing MapMeViewController 38 1 Linking the Map Kit and Core Location Frameworks 38 9 Go East, Young Programmer 39 0 ■Chapter 12 : Sending Mail 39 1 This Chapter’s Application 39 1 The MessageUI... 33 9 Retrieving Data Asynchronously 34 4 NSURLConnection Delegate Methods 34 5 Adding Asynchronous Retrieval to WebWorks 34 6 Request Types and Form Parameters 35 0 Specifying the HTTP Request Types 35 0 Form Parameters 35 1 Building the RequestTypes Application 35 3 404 Conclusion... 36 9 The Annotation Object 37 0 ix ■ CONTENTS The Annotation View 37 0 Adding and Removing Annotations 37 1 Selecting Annotations 37 2 Providing the Map View with Annotation Views 37 2 Reverse Geocoding 37 3 Building the MapMe Application 37 5 Declaring Outlets... iPhone 3 Development, started with the phrase, “What an amazing journey!” Well, it’s true We’re having a blast, making a lot of new friends and, above all, learning, learning, learning The iPhone SDK continues to evolve, and with each new release, it brings new concepts to explore and new design patterns to master As its name implies, More iPhone 3 Development assumes you’ve read Beginning iPhone 3. .. before you are taken to the start of this document Once you have a firm handle on Objective-C, you need to master the fundamentals of the iPhone SDK For that, you should check out the prequel to this book, Beginning iPhone 3 Development (Apress 2008): http://www.apress.com/book/view /1 430 216 2 63 What You Need Before You Can Begin Before you can write software for iPhone, you need a few things For starters,... you to download the iPhone SDK If you’re already a Registered iPhone Developer, go ahead and download the latest and greatest iPhone development tools, and skip ahead to the next section If you’re new to Apple’s Registered iPhone Developer programs, navigate to http://developer.apple.com /iphone/ , which will bring you to a page similar to that shown in Figure 1- 1 Just below the iPhone Dev Center banner,... Queue ’em Up 4 93 ■Chapter 15 : Debugging .495 The Debugger 496 Breakpoints 497 The GDB Console 5 13 Static Analysis 516 Specific Bugs 517 Overreleasing Memory 517 Infinite Recursion 5 23 Missed Outlet and Action Connections... Mac development, including Beginning iPhone 3 Development (Apress, 2009), Learn C on the Mac (Apress, 2008), The Macintosh Programming Primer series (Addison-Wesley, 19 92), and Ultimate Mac Programming (Wiley, 19 95) Dave loves the water and spends as much time as possible on it, in it, or near it He lives with his wife and three children in Virginia Jeff LaMarche is a Mac and iPhone developer with more . Actions and Outlets 33 1 Designing the Interface 33 3 Implementing the Stubs 33 5 Retrieving Data Using Foundation Objects 33 6 Retrieving Data Synchronously 33 9 The URL Request 33 9 Retrieving Data. and Insight More iPhone 3 Development Tackling iPhone SDK 3 Dave Mark | Jeff LaMarche RELATED TITLES By the best-selling authors of Beginning iPhone Development i More iPhone 3 Development: . Application 35 3 404 Conclusion Not Found 35 8 ■Chapter 11 : MapKit 35 9 This Chapter’s Application 36 0 Overview and Terminology 36 1 The Map View 36 2 Map Types 36 2 User Location 36 4 Coordinate