www.it-ebooks.info iOS SDK Programming: A Beginner’s Guide www.it-ebooks.info About the Authors James A. Brannan is a senior developer with more than 15 years of experience. He has developed using everything from AWK, to Visual Basic, to Java. His current interests are iOS, Blackberry, Android, and Adobe Flex/Flash. He is only $999,000 short of being the next app store overnight millionaire. He lives in Gaithersburg, Maryland, with his wife, two kids, two Macs, and bicycle. Blake Ward has a PhD in Computer Science from Carnegie Mellon University and has spent more than 30 years programming and managing software development. He has developed for a wide variety of mobile devices, ranging from the Apple Newton and Palm Pilot to RIM’s Blackberry, the iPhone and iPad, and Android phones. Blake has worked as a researcher and in management at Apple, Xerox PARC, and numerous startups. He is currently an independent iPhone and Android developer, available through www.iphoneappquotes.com. About the Technical Editor Born to golf, forced to work, Steven Weber, a Java Web Applications engineer, has ten years application development under his belt. He’s dabbled in iOS application development and released one corporate application since the launch of Apple’s App Store. He’s currently living it up in the Colorado Rockies. www.it-ebooks.info iOS SDK Programming: A Beginner’s Guide James A. Brannan Blake Ward New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto www.it-ebooks.info Copyright © 2011 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. ISBN: 978-0-07-175909-0 MHID: 0-07-175909-3 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-175908-3, MHID: 0-07-175908-5. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefi t of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative please e-mail us at bulksales@mcgraw-hill.com. Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGrawHill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. www.it-ebooks.info For Timothy Hill and Doctor Ronald Holt. This book is not some lofty political or anthropological treatise, but it was fun writing. —James For Bryce—the real author in our family. —Blake www.it-ebooks.info This page intentionally left blank www.it-ebooks.info vii Contents at a Glance 1 The iOS Software Development Kit (SDK) 1 2 A C Refresher 25 3 Just Enough Objective-C: Part One 41 4 Just Enough Objective-C: Part Two 61 5 Deploying to an iPhone, Debugging, and Testing 77 6 UIApplication and UIApplicationDelegate 109 7 UIView and UIViewController 127 8 UITabBar and UITabBarController 141 9 UINavigationBar and UINavigationController 159 10 Tables Using UITableView and UITableViewController 185 11 Activity Progress and Alerting Users 237 12 Controls—Part One: Using Buttons, Sliders, Switches, and Text Fields 263 www.it-ebooks.info viii iOS SDK Programming: A Beginner’s Guide 13 Controls—Part Two: Using Pickers and Using the Camera 295 14 Application Settings 329 15 Property Lists and Archiving 349 16 Data Persistence Using SQLite 371 17 Core Data 399 18 Multimedia 435 19 Universal Applications for the iPad 457 Index 485 www.it-ebooks.info ix Contents ACKNOWLEDGMENTS xvii INTRODUCTION xix 1 The iOS Software Development Kit (SDK) 1 The App Store 2 The Software Development Kit (SDK) 4 Paid Membership 5 Objective-C, Foundation Framework, Cocoa Touch, and UIKit 5 Cocoa Touch 6 Foundation Framework 6 The iOS Frameworks 6 Memory and Processor Speed 8 Small Screen 8 Security 9 Short-Lived Applications 9 Manual Memory Management 9 Relevant Documentation 9 Try This: Getting a Quick Start on iOS Development 10 Summary 23 2 A C Refresher 25 C Command-Line Programs 27 Try This: Creating a Simple C Program Using Xcode 27 www.it-ebooks.info [...]... Chapter 1: The iOS Software Development Kit (SDK) Framework Purpose Accelerate Accelerating math functions AddressBook Accessing user’s contacts AddressBookUI Displaying Addressbook AssetsLibrary Accessing user’s photos and videos AudioToolbox Audio data streams; playing and recording audio AudioUnit Audio units AVFoundation Objective-C interfaces for audio playback and recording CFNetwork WiFi and... garbage collection, and you must manage memory yourself Although manual memory management can be a pain, it is not a huge limitation Just be aware that forgetting to release an object is all too easy a mistake to make As you will see in Chapter 5, there are tools to help you track down and fix these errors Relevant Documentation Apple provides considerable online documentation You have access to that... 138 140 8 UITabBar and UITabBarController UITabBar, UITabBarController, UITabBarItem, and UITabBarControllerDelegate Try This: Using the Tab Bar Application Template Try This: Adding a Tab Bar Item to a Tab Bar Application Try This: Creating a Tab Bar Application from Scratch Try This: Allowing Users... iPhone and iPad device’s technology layers Cocoa Touch Objective-C Cocoa Layer Media iPhone OS C Layer Core Services Figure 1-4 The iPhone and iPad device’s programming layers www.it-ebooks.info 5 6 iOS SDK Programming: A Beginner’s Guide The Media layer is also rather low-level and contains C application programming interfaces (APIs) like OpenGL ES, Quartz, and Core Audio The Cocoa layer overlays the... layout and rendering CoreVideo Pipeline model for digital video EventKit Accessing user’s calendar EventKitUI Displaying standard system calendar ExternalAccessory Hardware accessory communication interfaces Foundation Cocoa foundation layer GameKit Peer-to-peer connectivity iAd Displaying advertisements ImageIO Reading and writing image data IOKit Low-level library for developing iPhone hardware attachments... Finally, Chapter 19 discusses the new SDK functionality available for the iPad and shows you how to create a universal application that will run on the iPhone or iPod touch but also take full advantage of the larger display on the iPad when available All of the framework functionality described in the earlier chapters applies to the iPad and the new iPhone 4, so this chapter focuses on how to layer... classes TIP See Apple’s Foundation Framework Reference for a complete listing of the classes and protocols provided by the Foundation framework NOTE If you are a Java programmer, think of the iOS s programming environment like this: Objective-C is equivalent to Java’s core syntax The Foundation framework is equivalent to Java’s core classes, such as ArrayList, Exception, HashMap, String, Thread, and... Membership is free and allows downloading the SDK and viewing all of the Apple documentation Once you’ve signed up, download and install Xcode and the iOS SDK from Apple’s Developer Connection Step-by-step installation instructions are available on Apple’s web site After installing the iOS SDK, the absolute next thing you should do is start Xcode and download the documentation—all the documentation (Figure... developing iPhone hardware attachments MapKit Embedding map in application and geocoding coordinates MediaPlayer Video playback MessageUI Composing e-mail messages OpenAL Positional audio library Table 1-1 Frameworks in iOS www.it-ebooks.info 7 8 iOS SDK Programming: A Beginner’s Guide Framework Purpose OpenGLES Embedded OpenGL (2-D and 3-D graphics rendering) QuartzCore Core animation QuickLook Previewing... think in terms of having a single window and swapping views based on interaction from your user Security You can only read or write to directories that are part of your application’s bundle or your application’s documents directory Areas accessible to your application are said to be in your application’s sandbox You cannot read files created by other applications unless the application places the files . 436 AudioServicesPlaySystemSound 437 AVAudioPlayer and AVAudioPlayerDelegate 438 Try This: Playing a Sound and an MP3 439 Media Player Framework 442 Media Data Classes. 140 8 UITabBar and UITabBarController 141 UITabBar, UITabBarController, UITabBarItem, and UITabBarControllerDelegate 142 Try This: Using the Tab Bar Application