www.it-ebooks.info www.it-ebooks.info FOURTH EDITION Learning Cocoa with Objective-C Jon Manning, Paris Buttfield-Addison, and Tim Nugent www.it-ebooks.info Learning Cocoa with Objective-C, Fourth Edition by Jon Manning, Paris Buttfield-Addison, and Tim Nugent Copyright © 2014 Jonathon Manning, Paris Buttfield-Addison, Tim Nugent All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://my.safaribooksonline.com) For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com Editors: Brian Jepson and Rachel Roumeliotis Production Editor: Melanie Yarbrough Copyeditor: Jasmine Kwityn Proofreader: Gillian McGarvey February 2014: Indexer: Lucie Haskins Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Rebecca Demarest Fourth Edition Revision History for the Fourth Edition: 2014-02-14: First release See http://oreilly.com/catalog/errata.csp?isbn=9781491901397 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Learning Cocoa with Objective-C, the image of an Irish setter, and related trade dress are trade‐ marks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-491-90139-7 [LSI] www.it-ebooks.info Table of Contents Preface xi Cocoa Development Tools The Mac and iOS Developer Programs Registering for a Developer Program Downloading Xcode from Apple Developer Getting Around in Xcode Creating Your First Cocoa Project The Xcode Interface Developing a Simple Objective-C Application Designing the Interface Connecting the Code Using the iOS Simulator 3 14 14 15 17 Object-Oriented Programming with Objective-C 21 Object-Oriented Programming Objects Inheritance Interfaces and Implementations Methods Messages Properties Protocols Class Extensions Modules Memory Management Reference Counting Automatic Reference Counting Object Graphs in Objective-C 21 22 23 23 24 25 26 29 30 31 33 33 33 34 iii www.it-ebooks.info The NSObject Lifecycle Allocation and Initialization Retain and Release Finalization and Deallocation 35 35 36 36 Foundation 37 Mutable and Immutable Objects Strings Creating Strings Working with Strings Comparing Strings Searching Strings Arrays Fast Enumeration Mutable Arrays Dictionaries NSValue and NSNumber Data Loading Data from Files and URLs Serialization and Deserialization Design Patterns in Cocoa Model-View-Controller Delegation Key-Value Observing 37 38 39 39 42 43 43 46 46 48 49 50 50 51 54 54 56 56 Applications on OS X and iOS 59 What Is an Application? Applications, Frameworks, Utilities, and More What Are Apps Composed Of? Using NSBundle to Find Resources in Applications The Application Lifecycle OS X Applications iOS Applications The Application Sandbox Application Restrictions 59 60 61 63 64 64 66 71 71 Graphical User Interfaces 75 Interfaces in OS X and iOS MVC and Application Design Nib Files Structure of a Nib File Storyboards iv | Table of Contents www.it-ebooks.info 75 76 76 77 81 Outlets and Actions How Nib Files Are Loaded Constructing an Interface Guidelines and Constraints Building an App with Nibs and Constraints UI Dynamics UI and Gravity Snapping UI Designing Interfaces for Both iOS and Core Animation Layers Animations 81 82 83 83 85 87 88 89 90 91 92 93 Blocks and Operation Queues 95 Blocks Block Syntax Block Lifecycles Methods with Block Parameters Blocks and Memory Management Modifying Local Variables from Inside Blocks with block Concurrency with Operation Queues Operation Queues and NSOperation Performing Work on Operation Queues Putting It All Together 95 96 97 99 100 100 101 102 102 104 Drawing Graphics in Views 109 How Drawing Works The Pixel Grid Retina Displays Pixels and Screen Points Drawing in Views Frame Rectangles Bounds Rectangles Building a Custom View Creating the Project Filling with a Solid Color Working with Paths Creating Custom Paths Multiple Subpaths Shadows Gradients Transforms 109 111 111 113 114 114 115 116 116 117 118 120 122 124 128 131 Table of Contents www.it-ebooks.info | v SpriteKit 133 Audio and Video 137 AV Foundation Playing Video with AVPlayer AVPlayerLayer Putting It Together AVPlayerView Playing Sound with AVAudioPlayer Speech Synthesis Working with the Photo Library Capturing Photos and Video from the Camera Building a Photo Application The Photo Library 137 138 139 139 143 146 147 148 148 150 152 Model Objects and Data Storage 155 Key-Value Coding Key-Value Observing Registering for Change Notifications Notifying Observers of Changes Notifications with NSNotification Preferences Registering Default Preferences Accessing Preferences Setting Preferences Working with the Filesystem Using NSFileManager File Storage Locations Working with the Sandbox Enabling Sandboxing Open and Save Panels Security-Scoped Bookmarks 156 158 158 160 160 161 162 163 164 164 166 169 169 169 170 171 10 Cocoa Bindings 173 Binding Views to Models A Single Bindings App Binding to Controllers Array and Object Controllers A More Complex Bindings App 173 174 177 178 179 11 Table Views and Collection Views 187 Data Sources and Delegates vi | 187 Table of Contents www.it-ebooks.info Table Views UITableView on iOS Sections and Rows Table View Controllers Table View Cells Implementing a Table View NSTableView on OS X Sorting a Table View NSTableView with Bindings Collection Views UICollectionView on iOS 188 188 189 189 190 193 196 200 201 202 202 12 Document-Based Applications 207 The NSDocument and UIDocument Classes Document Objects in MVC Kinds of Documents The Role of Documents Document-Based Applications on OS X Autosaving and Versions Representing Documents with NSDocument Saving Simple Data Saving More Complex Data Document-Based Applications on iOS 208 208 208 209 210 210 211 212 214 218 13 Networking 227 Connections NSURL NSURLRequest NSURLConnection NSURLSession NSURLResponse and NSHTTPURLResponse Building a Networked Application Bonjour Service Discovery Browsing for Shared iTunes Libraries Multipeer Connectivity 227 228 229 230 230 231 231 233 234 236 14 Working with the Real World 241 Working with Location Location Hardware The Core Location Framework Working with Core Location Geocoding 241 242 243 245 248 Table of Contents www.it-ebooks.info | vii Region Monitoring and iBeacons Locations and Privacy Maps Using Maps Annotating Maps Maps and Overlays Device Motion Working with Core Motion Printing Documents Printing on OS X Printing on iOS Game Controllers App Nap 251 252 253 253 254 255 256 257 261 262 263 264 267 15 Event Kit 269 Understanding Events Accessing the Event Store Accessing Calendars Accessing Events Working with Events Building an Events Application User Privacy 269 270 271 271 272 273 278 16 Instruments and the Debugger 281 Getting Started with Instruments The Instruments Interface Observing Data Adding Instruments from the Library Fixing Problems with Instruments Retain Cycles and Leaks Using the Debugger Setting Breakpoints Inspecting Memory Contents Working with the Debugger Console 282 282 284 286 286 292 295 295 299 299 17 Sharing and Notifications 301 Sharing Sharing on iOS Sharing on OS X Notifications Push Notifications Sending Push Notifications viii | 301 304 306 307 307 308 Table of Contents www.it-ebooks.info ...www.it-ebooks.info FOURTH EDITION Learning Cocoa with Objective- C Jon Manning, Paris Buttfield-Addison, and Tim Nugent www.it-ebooks.info Learning Cocoa with Objective- C, Fourth Edition by Jon Manning,... Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Learning Cocoa with Objective- C, the image of an Irish setter, and related trade dress are trade‐ marks of O’Reilly... development with Cocoa, Cocoa Touch, and Objective- C Audience We assume that you’re a reasonably capable programmer, but we don’t assume you’ve ever developed for iOS or OS X, or used Objective- C