Computers/Application Development/Apple Making Everything Easier!™ p p A S Get started making apps for i O Open the book and find: i iPhone and iPad with this O easy-to-use guide! • How to download and use S the latest SDK A ment There’s no time like the present to begin developing apps • How to use animation in p Develop for the iPhone and iPad. Discover the Apple iOS SDK and your app’s views p developer tools, and then dive right in to planning and • The differences between iPad coding your app’s interface and functionality. This handy and iPhone apps D guide leads you step-by-step through the app development • Tricks to fixing syntax errors as e process, including designing interfaces, creating storyboards, you go v and handling events, notifications, gestures, and more! • Using storyboards to create e your UI l • A primer — find out about the iOS development environment, o how to design a great user experience, and how to build apps • Tips on navigating in the p that get on the App Store Xcode® source editors m • Hands-on examples to jumpstart • Dive right in — download the iOS SDK and get started with the Apple developer tools your work e n • Step-by-step exercises to create • Get universal — understand the key differences between iPad real applications t and iPhone applications, and how to make universal apps • It’s all about the user — dive in to designing apps that work intuitively and please the user Cover Image: Toolbox ©iStockphoto.com/ AndrewJohnson; Map ©iStockphoto.com/boris64; Learn to: WiFi icon ©iStockphoto.com/Samarskaya; Rainbow • The next level — find out about storyboard files, view controllers, Flower © iStockphoto.com/chris_lemmens; Gear interface objects, gesture recognizers, and a whole lot more Icon: Wiley • Download the iOS SDK and use Apple’s developer tools Visit the companion website at www.dummies.com/ • Build a universal app for the iPad® extras/iosapplicationdevelopment for source code, all and iPhone® Go to Dummies.com® the figures from the book, and other information. for videos, step-by-step examples, • Make the most of the latest iOS how-to articles, or to shop! features in your app designs Jesse Feiler is a developer, consultant, and author specializing in Apple technologies. He is the creator of Minutes Machine for iPad, $34.99 USA / $41.99 CAN / £24.99 UK • Provide a great user experience and the meeting management app, and Saranac River Trail and is heard make your app stand out in the crowd regularly on WAMC Public Radio for the Northeast’s The Roundtable. ISBN:978-1-118-87105-8 53499 Jesse Feiler 9 781118871058 Feiler Author of iWork For Dummies®, 2nd Edition Get More and Do More at Dummies.com ® At home, at work, or on the go, Start with FREE Cheat Sheets Cheat Sheets include Dummies is here to help you • Checklists • Charts go digital! • Common Instructions • And Other Good Stuff! To access the Cheat Sheet created specifically for this book, go to www.dummies.com/cheatsheet/iosappdevelopment Get Smart at Dummies.com Dummies.com makes your life easier with 1,000s of answers on everything from removing wallpaper to using the latest version of Windows. Check out our • Videos • Illustrated Articles • Step-by-Step Instructions Plus, each month you can win valuable prizes by entering our Dummies.com sweepstakes. * Want a weekly dose of Dummies? Sign up for Newsletters on • Digital Photography • Microsoft Windows & Office • Personal Finance & Investing • Health & Wellness From eLearning to e-books, test prep to test banks, • Computing, iPods & Cell Phones language learning to video training, mobile apps, and more, • eBay • Internet Dummies makes learning easier. • Food, Home & Garden Find out “HOW” at Dummies.com www.facebook.com/fordummies www.twitter.com/fordummies *Sweepstakes not currently available in all countries; visit Dummies.com for official rules. iOS App Development by Jesse Feiler iOS App Development For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2014 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2013957974 ISBN 978-1-118-87105-8 (pbk); ISBN 978-1-118-87107-2 (ebk); ISBN 978-1-118-87110-2 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Table of Contents Introduction ................................................................. 1 A Bit of History .................................................................................................1 The iPhone stands alone .......................................................................2 Enter the App Store ...............................................................................2 The iPad joins the party ........................................................................3 The Plan for This Book ...................................................................................3 iOS and Xcode Game Changers ......................................................................4 About This Book ..............................................................................................5 Conventions Used in This Book .....................................................................5 Icons Used in This Book .................................................................................6 Foolish Assumptions .......................................................................................7 How This Book Is Organized ..........................................................................7 Part I: Getting Started ............................................................................8 Part II: Building RoadTrip .....................................................................8 Part III: Getting Your Feet Wet: Basic Functionality ..........................8 Part IV: The Model and the App Structure .........................................9 Part V: Adding the App Content ...........................................................9 Part VI: The Part of Tens .......................................................................9 Beyond the Book ...........................................................................................10 Where to Go from Here .................................................................................10 Part I: Getting Started ................................................ 11 Chapter 1: What Makes a Great iOS App . . . . . . . . . . . . . . . . . . . . . . . . 13 Figuring Out What Makes a Great iOS App .................................................14 Making your app work well ................................................................14 Handling networking, social media, and location ............................15 Designing a powerful and intuitive interface that disappears .......15 Using the iOS Platform to the Fullest ..........................................................16 Exploiting advantages of the system .................................................16 Accessing the Internet.........................................................................17 Knowing the location of the user .......................................................18 Tracking orientation and motion .......................................................18 Tracking users’ fingers on the screen ...............................................19 Playing content ....................................................................................19 Accessing information from Apple’s apps ........................................19 Copying, cutting, and pasting between apps ....................................20 Multitasking, background processing, and notifications................20 Living large on the big screen ............................................................21 Embracing Device Limitations .....................................................................21 Designing for fingers ............................................................................22 Balancing memory and battery life ...................................................22 iv iOS App Development For Dummies Why Develop iOS Apps? ................................................................................23 Developing with Apple’s Expectations in Mind .........................................24 Thinking About You, Apps, and Money ......................................................25 Enter the Cloud ..............................................................................................26 Developing an App the Right Way Using the Example App in This Book ........................................................................27 What’s Next ....................................................................................................27 Chapter 2: Getting to Know the SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Developing Using the SDK ............................................................................29 Using Xcode to Develop an App ..................................................................30 Creating an Xcode project ..................................................................31 Developing the app ..............................................................................31 The Workspace Window ...............................................................................33 Workspace areas ..................................................................................34 Displaying an area’s content ..............................................................36 The toolbar and Tab bar .....................................................................40 The Organizer window ........................................................................42 Chapter 3: The Nuts and Bolts of an Xcode Project . . . . . . . . . . . . . . . 45 Creating Your Project ...................................................................................45 Exploring Your Project .................................................................................50 The project ...........................................................................................50 The Project editor ................................................................................50 The Project navigator ..........................................................................53 Setting Your Xcode Preferences ..................................................................57 Building and Running Your Application .....................................................59 Building an app ....................................................................................61 The iPad’s Split views ..........................................................................63 The Log navigator ................................................................................64 Running in the Simulator ..............................................................................66 Interacting with your simulated hardware .......................................66 Making gestures ...................................................................................67 Uninstalling apps and resetting your device ....................................68 Living with the Simulator’s limitations .............................................69 Using Asset Catalogs .....................................................................................70 Adding the Image and Sound Resources and an App Icon .......................74 Part II: Building RoadTrip ........................................... 77 Chapter 4: Storyboards and the User Experience . . . . . . . . . . . . . . . . . 79 Introducing the Storyboard ..........................................................................80 Telling your story ................................................................................81 Working with object graphs ...............................................................83 Defining What You Want an App to Do: The RoadTrip App ....................84 Creating the Application Architecture ........................................................88 What You Add Where ....................................................................................89 v Table of Contents Using Frameworks .........................................................................................90 Using Design Patterns ...................................................................................91 The iOS design patterns ......................................................................92 The Model-View-Controller (MVC) design pattern ..........................92 Working with Windows and Views ..............................................................95 Looking out the window......................................................................95 Admiring the view ................................................................................96 The kinds of views you use.................................................................97 View Controllers — the Main Storyboard Players ..................................101 What About the Model? ..............................................................................104 It’s Not That Neat .........................................................................................105 Taking a Look at Other Frameworks .........................................................106 The Foundation framework ..............................................................106 The CoreGraphics framework ..........................................................106 Even more frameworks .....................................................................107 Understanding the MVC in the Project .....................................................107 Chapter 5: Creating the RoadTrip User Interface . . . . . . . . . . . . . . . . 111 Creating Your User Interface in the iPad Storyboard .............................111 It’s about the view controller ...........................................................112 Using Interface Builder to add the user elements .........................113 Working within the Utility Area .................................................................115 Inspector and Quick Help pane ........................................................115 Library pane .......................................................................................116 Understanding iPad Navigation .................................................................117 Adding a New View Controller ...................................................................121 Danger Will Robinson ........................................................................128 Adding an identifier to the view controller ....................................129 View Layout ..................................................................................................130 Adding the User Interface Objects ............................................................131 Autosizing with Auto Layout ............................................................136 Adding the Test Drive button ...........................................................141 Massaging the Template Code ...................................................................144 Getting Rid of Warnings ..............................................................................148 Creating the iPhone User Interface ...........................................................148 Chapter 6: The Runtime, Managing Memory, and Using Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Stepping Through the App Life Cycle .......................................................152 UIApplicationMain .............................................................................153 Handling events while your application is executing ...................158 Knowing what to do when the normal processing of your application is interrupted .............................................................160 An overview of the view controller life cycle .................................163 Working within the Managed Memory Model Design Pattern ...............164 Understanding memory management .............................................165 Using reference counting ..................................................................165 vi iOS App Development For Dummies Automatic Reference Counting (ARC) ......................................................167 Working with variable types according to ARC .............................169 Understanding the deadly retain cycle ...........................................170 Observing Low-Memory Warnings ............................................................172 The didReceiveMemoryWarning method .......................................172 The applicationDidReceiveMemoryWarning: method ..................172 The UIApplicationDidReceiveMemoryWarningNotification: notification ......................................................................................173 Picking the right memory-management strategy for your application .............................................................................173 Customizing the Behavior of Framework Classes ...................................174 Subclassing .........................................................................................174 The Delegation pattern .....................................................................175 Understanding Declared Properties ..........................................................176 What comprises a declared property .............................................176 Using dot syntax ................................................................................177 Setting attributes for a declared property......................................178 Writing your own accessors .............................................................180 Accessing instance variables with accessors ................................181 Hiding Instance Variables ...........................................................................181 Chapter 7: Working with the Source Editor . . . . . . . . . . . . . . . . . . . . . 183 Navigating in the Xcode Source Editors ...................................................183 Using the Jump bar ............................................................................186 Organizing your code using the #pragma mark statement ..........190 Using the Xcode Source Editor ..................................................................190 Using Live Issues and Fix-it ...............................................................192 Compiler warnings .............................................................................193 The Issue navigator ...........................................................................193 Accessing Documentation ..........................................................................195 Getting Xcode help ............................................................................195 The Organizer window ......................................................................198 The Help menu ...................................................................................199 Finding and Searching in Your Project .....................................................199 Using the Find command to locate an item in a file ......................199 Using the Search navigator to search your project or framework ............................................................200 Using the Symbol navigator ..............................................................201 You’re Finally Ready to Write Code! .........................................................202 Part III: Getting Your Feet Wet: Basic Functionality ... 203 Chapter 8: It’s (Finally) T ime to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Checking for Network Availability .............................................................205 Downloading the Reachability sample ............................................205 Adding the code to check for reachability .....................................208 vii Table of Contents Exploring the Changes in iOS 7 ..................................................................211 The dated interface ...........................................................................211 Losing the content .............................................................................212 Setting the Master View Controller Title ..................................................213 Understanding Autorotation ......................................................................214 Writing Bug-Free Code ................................................................................215 Working in the Debug area and Debug navigator ..........................216 Managing breakpoints .......................................................................218 What you’ll find in the Debug area ..................................................222 What you’ll find in the Debug navigator .........................................223 Displaying variables in the Source editor .......................................224 Tiptoeing through your program .....................................................225 Chapter 9: Adding Outlets and Actions to Your RoadTrip Code . . . . 227 Using Custom View Controllers .................................................................228 Adding the custom view controller .................................................228 Setting up the TestDriveController in the MainStoryboard for iPad ............................................................................................229 Understanding Outlets ................................................................................231 Adding Outlets .............................................................................................232 Opening the Assistant editor............................................................232 Creating the outlet .............................................................................234 The Connections inspector ..............................................................237 Working with the Target-Action Design Pattern ......................................239 Using the Target-Action pattern: It’s about controls ....................239 Adding an action ................................................................................241 How Outlets and Actions Work ..................................................................244 Update the iPhone storyboard file ..................................................244 Chapter 10: Adding Animation and Sound to Your App . . . . . . . . . . . 247 Understanding iOS Animation ....................................................................248 View geometry and coordinate systems .........................................248 Points versus pixels ...........................................................................248 A view’s size and position.................................................................249 Working with data structures ..........................................................250 Coordinating Auto Layout, Frames, and Constraints .............................250 Animating a View .........................................................................................251 Finally, More Code .......................................................................................252 Implementing the testDrive Method .........................................................252 Understanding Block Objects ....................................................................256 Rotating the Object .....................................................................................259 Working with Audio .....................................................................................261 Tracking Touches ........................................................................................269 Animating a Series of Images “In Place” ...................................................272 iPhone versus iPad ......................................................................................273 viii iOS App Development For Dummies Part IV: The Model and the App Structure .................. 275 Chapter 11: The Trip Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 What the Model Contains ...........................................................................277 Adding the Model Data ...............................................................................278 Using property lists ...........................................................................278 Adding a property list to your project ............................................280 Adding the First Two Model Classes .........................................................290 Understanding the Trip Interface ..............................................................292 Implementing the Trip Class ......................................................................294 Initializing objects ..............................................................................296 Invoking the superclass’s init method ............................................297 Initializing instance variables ...........................................................298 Returning self .....................................................................................299 Initializing the Destination Class ...............................................................300 Creating the Trip Object .............................................................................303 More Debugger Stuff ....................................................................................305 Chapter 12: Implementing the Master View Controller . . . . . . . . . . . 309 Setting Up a Custom View Controller for the iPad ..................................309 Adding a Background Image and Title ......................................................319 Updating the iPhone Storyboard File ........................................................321 Chapter 13: Working with Split View Controllers and the Master View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 The Problem with Using a Navigation Controller in Detail View ...........323 Using a Navigation Controller in the Master View ..................................326 Adding a Gesture Recognizer .....................................................................330 The Split View Controller ...........................................................................333 The UISplitViewController delegate ................................................335 Localization ........................................................................................340 Back to the main feature ...................................................................340 Adding the Toolbar .....................................................................................346 Adding the button when the view controller is replaced .............350 A Few More Tweaks to the MasterViewController ..................................354 And (a Little Extra) One More Thing .........................................................355 Don’t Forget the iPhone ..............................................................................356 Chapter 14: Finishing the Basic App Structure . . . . . . . . . . . . . . . . . . 357 Extending the iPad Storyboard to Add More Functionality to Your App ..............................................................................................358 Adding the Weather view controller ...............................................358 Adding the Events controller ...........................................................364 Adding the remaining controllers ....................................................367