MANNING J. Harrington B. Trebitowski C. Allen S. Appelcline Examples and solutions for iPhone & iPad www.it-ebooks.info Praise for the previous edition iPhone and iPad in Action Everything you need to know about these devices of the future! —Berndt Hamboeck, pmOne Apple should make this its official iPhone and iPad development book. —Jason Jung, Rockwell Gets you up to speed and developing in a snap. —Clint Tredway, Developed It Don’t launch Xcode without this book. —Ted Neward, Neward & Associates Exactly what iNeed for iPhone development. —Christopher Haupt, Webvanta.com It’s wonderful to have all the material that covers development for multiple Apple mobile devices in one volume. —Glenn Stokol, Oracle Corporation A good first place to start instead of wading through multiple Apple documents. —Gershon Kagan, Tegrity, Inc. Brandon, Christopher, and Shannon have created the quintessential book for iPhone and iPad development. —Daniel McGraw, Software Engineer Brandon Trebitowski will take you up and over the iOS learning curve with his enjoyable writing style and excellent example code. iPhone and iPad in Action is a must-have addition to your programming library—even for 20-year Objective-C veterans like me! —Andrew Stone, CEO of stone.com www.it-ebooks.info www.it-ebooks.info iOS 4 in Action EXAMPLES AND SOLUTIONS FOR iPHONE & iPAD JOCELYN HARRINGTON BRANDON TREBITOWSKI CHRISTOPHER ALLEN SHANNON APPELCLINE TECHNICAL EDITOR JAMES HATHEWAY MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2011 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editors: Maria Townsend 20 Baldwin Road Katharine Osborne PO Box 261 Copyeditor: Tiffany Taylor Shelter Island, NY 11964 Typesetter: Dottie Marsico Cover designer: Marija Tudor ISBN 9781617290015 Printed in the United States of America 12345678910–MAL – 16 15 14 13 12 11 www.it-ebooks.info v brief contents 1 ■ Introducing iOS 4 with iPhone and iPad 1 2 ■ Learning Objective-C 20 3 ■ Using Xcode 4 31 4 ■ Xcode and debugging 58 5 ■ Basic view controllers 75 6 ■ Monitoring events and actions 93 7 ■ Advanced view controllers 117 8 ■ Data: actions, preferences, and files 146 9 ■ Data: advanced techniques 166 10 ■ Positioning: accelerometers, location, and the compass 195 11 ■ Media: images and the camera 219 12 ■ Media: audio and recording 239 13 ■ Graphics: Quartz, Core Animation, and OpenGL 258 14 ■ The web: web views and internet protocols 287 15 ■ Peer-to-peer connections using Game Kit 311 16 ■ Using Event Kit on the iPhone and iPad 332 17 ■ Local and Push notification services 347 18 ■ The Map Kit framework 363 19 ■ In-app purchasing using Store Kit 378 20 ■ Making money with iAd 393 21 ■ Introducing multitasking 407 22 ■ Multitasking in depth 425 www.it-ebooks.info www.it-ebooks.info vii contents preface xvii acknowledgments xviii about this book xx about the cover illustration xxiv 1 Introducing iOS 4 with iPhone and iPad 1 1.1 All for one and one for all: the iOS platform 2 1.2 Understanding iPhone and iPad touch interaction 2 1.3 Getting ready for the SDK 4 Installing the SDK 4 ■ The anatomy of the SDK 6 1.4 Introducing iOS 8 The anatomy of iOS 8 ■ The object hierarchy of iOS 9 Windows and views 10 1.5 iOS’s methods 11 Object creation 11 ■ Memory management 13 ■ Event response 14 ■ Lifecycle management 16 1.6 How to make an application from an idea 16 The checklist 17 ■ What’s the category for your application? 17 Making money with your application 18 1.7 Summary 19 www.it-ebooks.info CONTENTSviii 2 Learning Objective-C 20 2.1 Introducing Objective-C 20 2.2 The message 22 Messages with arguments 22 ■ Nested messages 23 Message recipients 23 2.3 Class definition 24 The interface 24 ■ The implementation 25 Object instantiation 26 2.4 Properties 26 The purpose of properties 26 ■ Setting a property 27 Using the accessors 27 ■ The dot syntax 28 ■ Property complexities 28 2.5 The @ directive 28 2.6 Categories and protocols 29 The category 29 ■ The protocol 29 2.7 Wrapping up Objective-C 30 2.8 Summary 30 3 Using Xcode 4 31 3.1 Introducing Xcode 4 31 3.2 Using Xcode 4 to create the HelloWorld application 32 Creating a new project 32 ■ Getting familiar with the workspace window 35 3.3 Closer look at files under the Navigation area 36 HelloWorld folder 36 ■ Frameworks folder and Product folder 41 Building and running an application in Xcode 41 ■ Writing code for HelloWorld 42 3.4 Using Interface Builder in Xcode 4 45 The anatomy of Interface Builder 46 ■ Building the AppleStock application 48 3.5 Summary 57 4 Xcode and debugging 58 4.1 Creating a new class in Xcode 59 Creating a new class 59 ■ The header file 61 ■ The source code file 62 ■ Linking it in 63 www.it-ebooks.info CONTENTS ix 4.2 Creating objects with Interface Builder 64 Creating new nib files 64 ■ Initializing Interface Builder objects 65 ■ Accessing .xib files 66 ■ More tips under Xcode 67 4.3 Debugger and Instruments 68 Fix-it function 68 ■ Analyze 69 ■ Debugger essentials 69 Running Instruments from Xcode 72 4.4 Summary 74 5 Basic view controllers 75 5.1 The view controller family 76 5.2 The standard view controller 77 The anatomy of a view controller 77 ■ Creating a view controller 78 ■ Creating another view controller 78 Building up a view controller interface 79 ■ Using your view controller 80 5.3 The table view controller 84 The anatomy of a table view controller 85 ■ Creating a table view controller 85 ■ Building up a table interface 86 ■ Using your table view controller 91 5.4 Summary 92 6 Monitoring events and actions 93 6.1 An introduction to events 94 The responder chain 94 ■ Touches and events 95 6.2 A touching example: the event reporter 97 Setting things up in Interface Builder 98 ■ Preparing a view for touches 99 ■ Controlling your events 101 6.3 Other event functionality 103 Regulating events 103 ■ Other event methods and properties 104 6.4 An introduction to actions 105 The UIControl object 105 ■ Control events and actions 105 Using addTarget:action:forControlEvents: 107 6.5 Adding a button to an application 108 Using addTarget:action:forControlEvents: with a button 108 Using an IBAction with a button 109 www.it-ebooks.info [...]... multitasking ■ 41 2 Understanding the background state background state 41 4 21.3 41 2 Using fast app switching 41 3 ■ Opting out of the 41 4 Building a simple application for fast app switching 41 4 Updating the user interface in the view controller 41 6 21 .4 Task completion in the background Task-completion API background 41 9 21.5 41 8 ■ 41 8 Finishing a task in the Monitoring location changes in the background 42 0... audio playing in the background 43 7 Adding the UIBackgroundModes in Info.plist 43 7 Handling the remote-control events 43 8 22 .4 22.5 Building the background audio application Summary 44 1 appendix A appendix B appendix C appendix D iOS class reference 44 2 External sources and references 44 6 Publishing your application 44 8 Updating current applications for the iPad 45 1 index 45 9 43 9 www.it-ebooks.info preface... 232 ■ ■ 11.5 ■ Printing images 233 Printing workflow 233 Simulating printing 2 34 Creating a demo app-printing image 235 Launching the printer app on the Simulator 237 ■ ■ 11.6 12 Summary 238 Media: audio and recording 12.1 239 Playing audio from the iPod library 240 Retrieving audio items from the iPod media library 240 Getting information about an MPMediaItem 242 Playing media items using MPMusicPlayerController... service in the background 42 1 Monitoring significant location change 42 2 Monitoring region-based location change 42 3 ■ 21.6 22 Summary 42 4 Multitasking in depth 42 5 22.1 Using the location-monitoring service 42 6 Updating the UI when the app relaunches 42 6 significant-change location service 42 7 22.2 22.3 ■ Enabling the Building an audio-playing application with the Audio Toolbox framework 43 0 Enabling... 8.1 8.2 Accepting user actions 147 Maintaining user preferences 146 148 Creating your own preferences 149 settings 1 54 8.3 143 Opening files ■ Using the system 158 Accessing your bundle 159 Accessing other directories 160 Manipulating files 161 Filesaver: a UITextView example 163 ■ ■ 8 .4 Summary 165 www.it-ebooks.info xi CONTENTS 9 Data: advanced techniques 9.1 Using SQLite 166 167 Setting up an SQLite... 20.3 Supporting both portrait mode and landscape mode 397 How to handle advertisement downloading errors 40 0 Adding a delegate to ADBannerView’s view controller 40 1 Simulating event handling 40 2 www.it-ebooks.info xvi CONTENTS 20 .4 20.5 21 Going live with the application 40 4 Summary 40 6 Introducing multitasking 40 7 21.1 Overview of multitasking Application lifecycle 40 9 21.2 Background state 40 8 How to... MPMusicPlayerController 242 Example: creating a simple media player application 244 ■ ■ ■ 12.2 Recording audio 247 Initializing the audio recorder 247 Controlling the audio recorder 248 Responding to AVAudioRecorder events 249 ■ ■ 12.3 Playing sounds 249 Initializing the AVAudioPlayer 250 The AVAudioPlayerDelegate 250 Controlling the AVAudioPlayer 251 ■ ■ 12 .4 Example: creating a simple audio recording/playback... UIView 261 ■ 259 Drawing to a bitmap 262 www.it-ebooks.info xiii CONTENTS 13.3 Drawing paths 263 Finishing a path 13 .4 2 64 Drawing rectangles ■ Setting the graphical state 266 266 Setting colors 267 Making transformations 268 Setting clipping paths 270 Other settings 271 Managing the state 272 ■ ■ 13.5 Advanced drawing in Quartz 272 Drawing gradients 272 Drawing images 2 74 Drawing words 275 What we... views and internet protocols 287 14. 1 14. 2 14. 3 The hierarchy of the internet Low-level networking 288 Working with URLs 290 287 Creating an NSURL 290 Building an NSURLRequest Manipulating HTML data by hand 291 ■ 14. 4 Using UIWebView 291 292 Calling up the web view 292 Managing the web view delegate 2 94 Thumbnails: a web view example 2 94 ■ ■ 14. 5 Parsing XML 296 Starting up NSXMLParser 297 Acting as a... computer produced in the last couple of decades In addition to some changes to existing interfaces, the input interface introduces a number of new touches (one-fingered input) and gestures (two-fingered input), as described in table 1.1 www.it-ebooks.info 4 CHAPTER 1 Table 1.1 Introducing iOS 4 with iPhone and iPad iPhone and iPad touches and gestures allow you to accept user input in new ways Input Type . 42 3 .4 Using Interface Builder in Xcode 4 45 The anatomy of Interface Builder 46 ■ Building the AppleStock application 48 3.5 Summary 57 4 Xcode and debugging 58 4. 1 Creating a new class in. 41 3 ■ Opting out of the background state 41 4 21.3 Using fast app switching 41 4 Building a simple application for fast app switching 41 4 Updating the user interface in the view controller 41 6 21 .4. controllers 144 7.7 Summary 145 8 Data: actions, preferences, and files 146 8.1 Accepting user actions 147 8.2 Maintaining user preferences 148 Creating your own preferences 149 ■ Using the system