iOS 9 SDK Development Creating iPhone and iPad Apps with Swift by Chris Adamson with Janie Clayton Version: P2.0 (August 2016) Copyright © 2016 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trust—you can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks. 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 Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. About the Pragmatic Bookshelf The Pragmatic Bookshelf is an agile publishing company. We’re here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com. Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while it’s still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. We’re here to make your life easier. New Book Announcements Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog. About Ebook Formats If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/adios3, the book's homepage. Thanks for your continued support, Dave Thomas and Andy Hunt The Pragmatic Programmers The team that produced this book includes: Rebecca Gulick (editor), Potomac Indexing, LLC (indexer), Liz Welch (copyeditor), Dave Thomas (layout), Janet Furlow (producer) For customer support, please contact [email protected]. For international rights, please contact [email protected]. Table of Contents Acknowledgments Preface About This Edition So Here’s the Plan Expectations and Technical Requirements Online Resources And Here We Go Part I. Coding in Swift 1. Playing with Xcode Tooling Up with Xcode Messing Around in a Playground Getting Serious on the Playground Digging Into Documentation What We’ve Learned 2. Starting with Swift The Swift Programming Language Using Variables and Constants Counting with Numeric Types Storing Text in Strings Packaging Data in Collections Looping and Branching: Control Flow Maybe It’s There, Maybe It Isn’t: Optionals What We’ve Learned 3. Swift with Style Creating Classes Returning Tuples Building Lightweight Structures Listing Possibilities with Enumerations Handling Errors the Swift 2.0 Way What We’ve Learned Part II. Creating the App 4. Building User Interfaces Our First Project The Xcode Window Building Our User Interface Autolayout What We’ve Learned 5. Connecting the UI to Code Making Connections Coding the Action The iOS Programming Stack Building Views with UIKit Managing an Object’s Properties What We’ve Learned 6. Testing the App Unit Tests How Tests Work in Xcode Test-Driven Development Creating Tests Testing Asynchronously User Interface Testing Running and Testing on the Device What We’ve Learned 7. Working with Tables Tables on iOS Table Classes Creating and Connecting Tables Filling In the Table Customizing Table Appearance Cell Reuse Custom Table Cells Pull-to-Refresh What We’ve Learned 8. Managing Time with Closures Setting Up Twitter API Calls Encapsulating Code in Closures Using the Twitter Account Making a Twitter API Request Parsing the Twitter Response What We’ve Learned 9. Doing Two Things at Once with Closures Grand Central Dispatch Concurrency and UIKit Do-It-Yourself Concurrency What We’ve Learned Part III. Evolving the App 10. Managing the App’s Growth Working with Multiple View Controllers Refactoring in Xcode Making the Twitter Code More General Purpose Trying Out Our Function What We’ve Learned 11. Moving Between View Controllers Navigation Controllers The Navigation Bar Navigating Between View Controllers Using the Storyboard Segue Sharing Data Between View Controllers Modal Navigation Exit Segues What We’ve Learned 12. Making the Most of Big Screens Split Views on iPad Split Views on the iPhone Size Classes and the iPhone 6 What We’ve Learned 13. Handling Touch Gestures Gesture Recognizers Pinching and Panning Affine Transformations Transforming the Image View Subview Clipping What We’ve Learned 14. Viewing and Editing Photos Photo Assets and PHAsset Class Fetching Our Assets Core Image What We’ve Learned Part IV. Beyond the App 15. Interacting with iOS and Other Apps The App Life Cycle Opening via URLs App Extensions Creating a Keyboard Extension Bundling Shared Code in Frameworks What We’ve Learned 16. Fixing the App When It Breaks NSLog(): The First Line of Defense Against Bugs Breakpoints Setting Up Your Debugging Environment What We’ve Learned 17. Publishing and Maintaining the App Getting with the Program Preparing the App for Submission Uploading the App Testing with TestFlight Publishing and Beyond Next Steps What We’ve Learned Copyright © 2016, The Pragmatic Bookshelf. Acknowledgments One of these years, we’ll be able to do a book where the information stays good for longer than a year, right? If so, apparently it won’t be about iOS. Once again, Apple’s annual update of its mobile OS and tools has unleashed a torrent of changes. At this point, it’s not even a question of leaving last year’s book on the shelf for a while longer; last year’s code doesn’t even compile anymore. So, for this book being in your hands or on your screen, thanks start with the Pragmatic Programmers. Working with the Prags themselves, Dave and Andy, and Susannah Pfalzer, recognized the need for the Prags introductory book on iOS to become an annual thing, if it’s to be a viable title and serve as a prerequisite for other Prags iOS titles. Together, we worked out a plan that would make for a book with enough new content and revisions to be worthy of a new release, without killing the primary author by trying to get 300 pages out of him in five months (while holding down a day job). Of course, that still wouldn’t have been possible without the support of editor Rebecca Gulick, who helped keep everything on track, caught lots of mistakes, and had a keen ear for when the prose needed to stop info-dumping and work more intuitively with the reader. Thanks also go out to friends and colleagues in the Mac/iOS (and watchOS and tvOS…) development community, who helped out with questions, feedback, and encouragement on Twitter and IRL at conferences like WWDC and CocoaConf. Speaking of CocoaConf, thanks as always to Dave Klein and his family for putting on that traveling event, which is still my favorite way to meet fellow developers and catch up on the state of the art of iOS programming, outside of Apple’s official channels. Speaking of conferences, Janie spent most of the summer and fall putting
Description: