Programming iOS 12, Ninth Edition
by Matt Neuburg
Copyright 2019 Matt Neuburg. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Rachel Roumeliotis
- Production Editor: Melanie Yarbrough
- Proofreader: OReilly Production Services
- Indexer: Matt Neuburg
- Cover Designer: Randy Comer
- Interior Designer: David Futato
- Illustrator: Matt Neuburg
- May 2011: First Edition
- March 2012: Second Edition
- March 2013: Third Edition
- December 2013: Fourth Edition
- December 2014: Fifth Edition
- November 2015: Sixth Edition
- November 2016: Seventh Edition
- December 2017: Eighth Edition
- October 2018: Ninth Edition
Revision History for the Ninth Edition:
- 2018-10-04: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781492044635 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Programming iOS 12, the cover image of a kingbird, and related trade dress are trademarks of OReilly Media, Inc.
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 OReilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-04463-5
[LSI]
Preface
Aut lego vel scribo; doceo scrutorve sophian.
Sedulius Scottus
On June 2, 2014, Apples WWDC keynote address ended with a shocking announcement: We have a new programming language. This came as a huge surprise to the developer community, which was accustomed to Objective-C, warts and all, and doubted that Apple could ever possibly relieve them from the weight of its venerable legacy. The developer community, it appeared, had been wrong.
Having picked themselves up off the floor, developers immediately began to consider this new language Swift studying it, critiquing it, and deciding whether to use it. My own first move was to translate all my existing iOS apps into Swift; this was enough to convince me that Swift deserved to be, and probably would be, adopted by new students of iOS programming, and that my books, therefore, should henceforth assume that readers are using Swift.
Therefore, Swift is the programming language used throughout this book. Still, the reader may also need some awareness of Objective-C (including C). The Foundation and Cocoa APIs, the built-in commands with which your code must interact in order to make anything happen on an iOS device, are still written in C and Objective-C. In order to interact with them, you might have to know what those languages would expect.
The Scope of This Book
Programming iOS 12 is actually the second of a pair with my other book, iOS 12 Programming Fundamentals with Swift; it picks up exactly where the other book leaves off. They complement and supplement one another. The two-book architecture should, I believe, render the size and scope of each book tractable for readers. Together, they provide a complete grounding in the knowledge needed to begin writing iOS apps; thus, when you do start writing iOS apps, youll have a solid and rigorous understanding of what you are doing and where you are heading. If writing an iOS program is like building a house of bricks, iOS 12 Programming Fundamentals with Swift teaches you what a brick is and how to handle it, while Programming iOS 12 hands you some actual bricks and tells you how to assemble them.
Programming iOS 12, like Homers Iliad, begins in the middle of the story, with the reader jumping with all four feet into views and view controllers.Discussion of the Swift programming language, as well as the Xcode IDE (including the nature of nibs, outlets, and actions, and the mechanics of nib loading), plus the fundamental conventions, classes, and architectures of the Cocoa Touch framework (including delegation, the responder chain, keyvalue coding, keyvalue observing, memory management, and so on),has been relegated to iOS 12 Programming Fundamentals with Swift.
So if something appears to be missing from this book, thats why! If you start reading Programming iOS 12 and wonder about such unexplained matters as Swift language basics, the UIApplicationMain
function, the nib-loading mechanism, Cocoa patterns of delegation and notification, and retain cycles, wonder no longer I dont explain them here because I have already explained them in iOS 12 Programming Fundamentals with Swift. If youre not sufficiently conversant with those topics, Id suggest that you might want to read that book first; you will then be completely ready for this one.
Heres a summary of the major sections of Programming iOS 12:
describes views, the fundamental units of an iOS apps interface. Views are what the user can see and touch in an iOS app. To make something appear before the users eyes, you need a view. To let the user interact with your app, you need a view. This part of the book explains how views are created, arranged, drawn, layered, animated, and touched.
starts by discussing view controllers. Perhaps the most important aspect of iOS programming, view controllers enable views to come and go coherently within the interface, thus allowing a single-windowed app running on what may be a tiny screen to contain multiple screens of material. View controllers are used to manage interface and to respond to user actions; most of your apps code will be in a view controller. This part of the book talks about how view controllers work, and the major built-in types of view controller that iOS gives you. It also describes every kind of view provided by the Cocoa framework the primary building blocks with which youll construct an apps interface.
surveys the most commonly used frameworks provided by iOS. These are clumps of code, sometimes with built-in interface, that are not part of your app by default, but are there for the asking if you need them, allowing you to work with such things as sound, video, user libraries, maps, and the devices sensors.
wraps up the book with some miscellaneous but significant topics: files, networking, threading, and how to implement undo.
summarizes the basic lifetime event messages sent to your app delegate.
catalogs some useful Swift utility functions that Ive written. My example code takes advantage of these functions, but they arent built into iOS, so you should keep an eye on this appendix, consulting it whenever a mysterious method name appears.