ebook img

The iOS Programming : Big Nerd Ranch Guide, PDF

528 Pages·2020·30.728 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The iOS Programming : Big Nerd Ranch Guide,

iOS Programming: The Big Nerd Ranch Guide by Christian Keur and Aaron Hillegass Copyright © 2020 Big Nerd Ranch All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, contact Big Nerd Ranch 200 Arizona Ave NE, Suite 200 Atlanta, GA 30307 (770) 817-6373 https://www.bignerdranch.com [email protected] The 10-gallon hat is a trademark of Big Nerd Ranch. Exclusive worldwide distribution of the English edition of this book by Pearson Technology Group 800 East 96th Street Indianapolis, IN 46240 USA http://www.informit.com The authors and publisher have taken care in writing and printing this book but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. App Store, Apple, Cocoa, Cocoa Touch, Finder, Instruments, iCloud, iPad, iPhone, iPod, iPod touch, iTunes, Keychain, Mac, macOS, Multi-Touch, Objective-C, Quartz, Retina, Safari, and Xcode are trademarks of Apple, Inc., registered in the U.S. and other countries. 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 the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.   ISBN-10  0135265533 ISBN-13  978-0135265536 Seventh edition, first printing, March 2020 Release D.7.1.1 Acknowledgments While our names appear on the cover, many people helped make this book a reality. We would like to take this chance to thank them. • First and foremost we would like to thank Joe Conway for his work on the early editions of this book. He authored the first three editions and contributed greatly to the fourth edition as well. Many of the words in this book are still his, and for that, we are very grateful. • A couple other people went above and beyond with their help on this book. They are Amit Bijlani, Chris Downie, Chris Morris, Jacob Bullock, Juan Pablo Claude, Mikey Ward, and Zachary Waldowski. • The other instructors who teach the iOS Bootcamp feed us a never-ending stream of suggestions and corrections. Over the years, they have included Ben Scheirman, Bolot Kerimbaev, Brian Hardy, Chris Downie, Chris Morris, Drew Kreuzman, Gabe Hoffman, JJ Manton, Jacob Bullock, John Gallagher, Jonathan Blocksom, Joseph Dixon, Juan Pablo Claude, Mark Dalrymple, Matt Bezark, Matt Mathias, Mike Zornek, Mikey Ward, Pouria Almassi, Robert Edwards, Rod Strougo, Scott Ritchie, Step Christopher, Thomas Ward, TJ Usiyan, Tom Harrington, and Zachary Waldowski. These instructors were often aided by their students in finding book errata, so many thanks are due to all the students who attend the iOS Bootcamp. • Thanks to all the employees at Big Nerd Ranch who helped review the book, provided suggestions, and found errata. • Our tireless editor, Elizabeth Holaday, took our distracted mumblings and made them into readable prose. • Simone Payment jumped in to provide proofing. • Ellie Volckhausen designed the cover. (The photo is of the bottom bracket of a bicycle frame.) • Chris Loper at IntelligentEnglish.com designed and produced the print and ebook versions of the book. • The amazing team at Pearson Technology Group that has patiently guided us through the business end of book publishing. The final and most important thanks goes to our students, whose questions inspired us to write this book and whose frustrations inspired us to make it clear and comprehensible. iii Table of Contents Introduction ................................................................................................................... xiii Prerequisites .......................................................................................................... xiii What Has Changed in the Seventh Edition? ................................................................ xiii Our Teaching Philosophy ......................................................................................... xiv How to Use This Book ............................................................................................. xv How This Book Is Organized .................................................................................... xv Style Choices ........................................................................................................ xvii Typographical conventions .............................................................................. xvii Necessary Hardware and Software ............................................................................ xvii 1. A Simple iOS Application .............................................................................................. 1 Creating an Xcode Project .......................................................................................... 2 Model-View-Controller ............................................................................................... 6 Designing Quiz ......................................................................................................... 7 Interface Builder ....................................................................................................... 8 Building the Interface ................................................................................................ 9 Creating view objects ....................................................................................... 10 Configuring view objects .................................................................................. 12 Running on the simulator .................................................................................. 14 A brief introduction to Auto Layout ................................................................... 15 Making connections ......................................................................................... 19 Creating the Model Layer ......................................................................................... 25 Implementing action methods ............................................................................ 26 Loading the first question ................................................................................. 26 Building the Finished Application .............................................................................. 27 Application Icons .................................................................................................... 28 Launch Screen ........................................................................................................ 31 For the More Curious: Running an Application on a Device ............................................ 32 2. The Swift Language ..................................................................................................... 35 Types in Swift ........................................................................................................ 36 Using Standard Types ............................................................................................... 37 Inferring types ................................................................................................ 39 Specifying types .............................................................................................. 39 Literals and subscripting ................................................................................... 41 Initializers ...................................................................................................... 42 Properties ....................................................................................................... 43 Instance methods ............................................................................................. 43 Optionals ................................................................................................................ 44 Subscripting dictionaries ................................................................................... 46 Loops and String Interpolation ................................................................................... 46 Enumerations and the Switch Statement ...................................................................... 48 Enumerations and raw values ............................................................................. 49 Closures ................................................................................................................. 50 Exploring Apple’s Swift Documentation ...................................................................... 51 3. Views and the View Hierarchy ....................................................................................... 53 View Basics ............................................................................................................ 54 v iOS Programming The View Hierarchy ................................................................................................. 54 Creating a New Project ............................................................................................. 56 Views and Frames ................................................................................................... 57 Customizing the labels ..................................................................................... 65 The Auto Layout System .......................................................................................... 68 The alignment rectangle and layout attributes ....................................................... 68 Constraints ..................................................................................................... 69 Adding constraints in Interface Builder ............................................................... 71 Intrinsic content size ........................................................................................ 74 Misplaced views .............................................................................................. 75 Adding more constraints ................................................................................... 76 Challenges .............................................................................................................. 78 Bronze Challenge: More Auto Layout Practice ............................................................. 79 Silver Challenge: Adding a Gradient Layer .................................................................. 79 Gold Challenge: Spacing Out the Labels ..................................................................... 80 For the More Curious: Retina Display ......................................................................... 81 4. View Controllers .......................................................................................................... 83 The View of a View Controller .................................................................................. 84 Setting the Initial View Controller .............................................................................. 85 Tab Bar Controllers .................................................................................................. 88 Tab bar items .................................................................................................. 91 Loaded and Appearing Views .................................................................................... 94 Refactoring in Xcode ....................................................................................... 96 Accessing subviews ......................................................................................... 98 Interacting with View Controllers and Their Views ........................................................ 98 Bronze Challenge: Another Tab ................................................................................. 99 Silver Challenge: Different Background Colors ............................................................. 99 5. Programmatic Views ................................................................................................... 101 Creating a View Programmatically ............................................................................ 103 Programmatic Constraints ........................................................................................ 104 Anchors ........................................................................................................ 105 Activating constraints ..................................................................................... 106 Layout guides ................................................................................................ 107 Margins ........................................................................................................ 108 Explicit constraints ......................................................................................... 109 Programmatic Controls ........................................................................................... 110 Bronze Challenge: Points of Interest ......................................................................... 112 Silver Challenge: Rebuild the Conversion Interface ...................................................... 112 For the More Curious: NSAutoresizingMaskLayoutConstraint ....................................... 113 6. Text Input and Delegation ............................................................................................ 115 Text Editing .......................................................................................................... 116 Keyboard attributes ........................................................................................ 121 Responding to text field changes ...................................................................... 122 Dismissing the keyboard ................................................................................. 124 Implementing the Temperature Conversion ................................................................. 126 Number formatters ......................................................................................... 129 Delegation ............................................................................................................ 130 Conforming to a protocol ................................................................................ 130 vi iOS Programming Using a delegate ............................................................................................ 131 More on protocols .......................................................................................... 133 Bronze Challenge: Disallow Alphabetic Characters ...................................................... 134 Silver Challenge: Displaying the User’s Region ........................................................... 134 7. Internationalization and Localization .............................................................................. 135 Internationalization ................................................................................................. 138 Formatters .................................................................................................... 138 Base internationalization ................................................................................. 142 Preparing for localization ................................................................................ 143 Localization .......................................................................................................... 150 NSLocalizedString and strings tables ................................................................. 153 Bronze Challenge: Another Localization .................................................................... 157 For the More Curious: Bundle’s Role in Internationalization .......................................... 157 For the More Curious: Importing and Exporting as XLIFF ............................................ 158 8. Debugging ................................................................................................................ 159 A Buggy Project .................................................................................................... 159 Debugging Basics .................................................................................................. 161 Interpreting console messages .......................................................................... 161 Fixing the first bug ........................................................................................ 164 Caveman debugging ....................................................................................... 165 The Xcode Debugger: LLDB ................................................................................... 167 Setting breakpoints ......................................................................................... 167 Stepping through code .................................................................................... 169 The LLDB console ......................................................................................... 178 9. UITableView and UITableViewController ....................................................................... 181 Beginning the LootLogger Application ...................................................................... 182 UITableViewController ........................................................................................... 184 Subclassing UITableViewController .................................................................. 185 Creating the Item Class ........................................................................................... 187 Custom initializers ......................................................................................... 187 UITableView’s Data Source ..................................................................................... 189 Giving the controller access to the store ............................................................. 191 Implementing data source methods ................................................................... 193 UITableViewCells .................................................................................................. 194 Creating and retrieving UITableViewCells .......................................................... 196 Reusing UITableViewCells .............................................................................. 198 Editing Table Views ............................................................................................... 200 Editing mode ................................................................................................ 200 Adding rows ................................................................................................. 205 Deleting rows ................................................................................................ 208 Moving rows ................................................................................................. 209 Design Patterns ...................................................................................................... 211 Bronze Challenge: Sections ..................................................................................... 212 Silver Challenge: Constant Rows .............................................................................. 212 Gold Challenge: Favorite Items ................................................................................ 212 10. Subclassing UITableViewCell ..................................................................................... 213 Creating ItemCell ................................................................................................... 214 Exposing the Properties of ItemCell .......................................................................... 216 vii iOS Programming Using ItemCell ...................................................................................................... 218 Dynamic Cell Heights ............................................................................................ 219 Dynamic Type ....................................................................................................... 220 Responding to user changes ............................................................................. 223 Bronze Challenge: Cell Colors ................................................................................. 224 Silver Challenge: Long Item Names .......................................................................... 224 11. Stack Views ............................................................................................................. 225 Using UIStackView ................................................................................................ 227 Implicit constraints ......................................................................................... 229 Stack view distribution .................................................................................... 231 Nested stack views ......................................................................................... 232 Stack view spacing ......................................................................................... 232 Segues ................................................................................................................. 234 Hooking Up the Content ......................................................................................... 235 Passing Data Around .............................................................................................. 242 Bronze Challenge: More Stack Views ........................................................................ 243 12. Navigation Controllers ............................................................................................... 245 UINavigationController ........................................................................................... 247 Navigating with UINavigationController .................................................................... 251 Appearing and Disappearing Views ........................................................................... 252 Dismissing the Keyboard ......................................................................................... 253 Event handling basics ..................................................................................... 254 Dismissing by pressing the Return key .............................................................. 254 Dismissing by tapping elsewhere ...................................................................... 256 UINavigationBar .................................................................................................... 258 Adding buttons to the navigation bar ................................................................. 261 Bronze Challenge: Displaying a Number Pad .............................................................. 264 Silver Challenge: A Different Back Button Title .......................................................... 264 Gold Challenge: Pushing More View Controllers ......................................................... 264 13. Saving, Loading, and Scene States ............................................................................... 265 Codable ................................................................................................................ 266 Property Lists ........................................................................................................ 267 Error Handling ...................................................................................................... 269 Application Sandbox .............................................................................................. 271 Constructing a file URL .................................................................................. 272 Scene States and Transitions .................................................................................... 273 Persisting the Items ................................................................................................ 277 Notification center .......................................................................................... 278 Saving the Items ............................................................................................ 280 Loading the Items .......................................................................................... 282 Bronze Challenge: Throwing Errors .......................................................................... 283 Gold Challenge: Support Multiple Windows ............................................................... 283 For the More Curious: Manually Conforming to Codable .............................................. 284 For the More Curious: Scene State Transitions ............................................................ 286 For the More Curious: The Application Bundle ........................................................... 288 14. Presenting View Controllers ....................................................................................... 291 Adding a Camera Button ......................................................................................... 294 Alert Controllers .................................................................................................... 299 viii iOS Programming Presentation Styles ................................................................................................. 302 15. Camera ................................................................................................................... 305 Displaying Images and UIImageView ........................................................................ 306 Taking Pictures and UIImagePickerController ............................................................. 308 Creating a UIImagePickerController .................................................................. 308 Setting the image picker’s delegate ................................................................... 312 Presenting the image picker modally ................................................................. 313 Permissions ................................................................................................... 314 Saving the image ........................................................................................... 316 Creating ImageStore ............................................................................................... 317 Giving View Controllers Access to the Image Store ..................................................... 318 Creating and Using Keys ......................................................................................... 320 Persisting Images to Disk ........................................................................................ 322 Loading Images from the ImageStore ........................................................................ 324 Bronze Challenge: Editing an Image ......................................................................... 325 Silver Challenge: Removing an Image ....................................................................... 325 For the More Curious: Navigating Implementation Files ............................................... 325 // MARK: ..................................................................................................... 327 16. Adaptive Interfaces ................................................................................................... 329 Size Classes .......................................................................................................... 329 Modifying traits for a specific size class ............................................................ 330 Adapting to Dark Mode .......................................................................................... 336 Adding colors to the Asset Catalog ................................................................... 340 Using custom dynamic colors .......................................................................... 342 Bronze Challenge: Stacked Text Field and Labels ........................................................ 347 17. Extensions and Container View Controllers ................................................................... 349 Starting Mandala ................................................................................................... 350 Creating the model types ................................................................................. 350 Adding resources to the Asset Catalog ............................................................... 351 Extensions ............................................................................................................ 353 Creating a custom container view controller ............................................................... 356 Creating the MoodSelectionViewController ........................................................ 357 Creating the MoodListViewController ............................................................... 365 Handling the embed segue ............................................................................... 368 18. Custom Controls ....................................................................................................... 371 Creating a Custom Control ...................................................................................... 372 Relaying actions ............................................................................................ 375 Using the Custom Control ....................................................................................... 376 Updating the Interface ............................................................................................ 377 Adding the Highlight View ...................................................................................... 379 Bronze Challenge: More Access Control .................................................................... 381 19. Controlling Animations .............................................................................................. 383 Property Animators ................................................................................................ 384 Basic animations ............................................................................................ 384 Timing functions ............................................................................................ 385 Spring animations .......................................................................................... 386 Animating Colors ................................................................................................... 387 Animating a Button ................................................................................................ 389 ix iOS Programming 20. Web Services ........................................................................................................... 391 Starting the Photorama Application ........................................................................... 392 Building the URL .................................................................................................. 394 Formatting URLs and requests ......................................................................... 394 URLComponents ........................................................................................... 395 Sending the Request ............................................................................................... 398 URLSession .................................................................................................. 399 Modeling the Photo ................................................................................................ 402 JSON Data ........................................................................................................... 402 JSONDecoder and JSONEncoder ...................................................................... 403 Parsing JSON data ......................................................................................... 403 Enumerations and Associated Values ......................................................................... 405 Passing the Photos Around ...................................................................................... 406 Downloading and Displaying the Image Data ............................................................. 412 The Main Thread ................................................................................................... 414 Bronze Challenge: Printing the Response Information .................................................. 416 Silver Challenge: Fetch Recent Photos from Flickr ...................................................... 416 For the More Curious: HTTP ................................................................................... 417 21. Collection Views ...................................................................................................... 419 Displaying the Grid ................................................................................................ 420 Collection View Data Source ................................................................................... 421 Customizing the Layout .......................................................................................... 424 Creating a Custom UICollectionViewCell .................................................................. 427 Downloading the Image Data ................................................................................... 432 Image caching ............................................................................................... 436 Navigating to a Photo ............................................................................................. 437 Bronze Challenge: Horizontal Scrolling ..................................................................... 441 Silver Challenge: Updated Item Sizes ........................................................................ 441 22. Core Data ................................................................................................................ 443 Object Graphs ....................................................................................................... 443 Entities ................................................................................................................. 444 Modeling entities ........................................................................................... 445 NSManagedObject and subclasses .................................................................... 450 NSPersistentContainer ............................................................................................. 451 Updating Items ...................................................................................................... 452 Inserting into the context ................................................................................. 452 Saving changes .............................................................................................. 454 Updating the Data Source ....................................................................................... 455 Fetch requests and predicates ........................................................................... 455 Silver Challenge: Photo View Count ......................................................................... 460 For the More Curious: The Core Data Stack ............................................................... 460 NSManagedObjectModel ................................................................................. 460 NSPersistentStoreCoordinator ........................................................................... 460 NSManagedObjectContext ............................................................................... 460 23. Core Data Relationships ............................................................................................ 461 Relationships ......................................................................................................... 462 Adding Tags to the Interface .................................................................................... 465 Background Tasks .................................................................................................. 476 x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.