Programming iOS 9
Matt Neuburg
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 examine 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, for all its faults, Swift deserved to 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. Nevertheless, the reader will 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 have to know what those languages would expect. For example, in order to pass a Swift array where an Objective-C NSArray is expected, you need to know what consitutes an object acceptable as an element of an Objective-C NSArray.
If you dont already know the Swift language and how it interacts with Objective-C, youll want to start with my other book, iOS 9 Programming Fundamentals with Swift . It is both an introduction and a companion to Programming iOS 9 . Like Homers Iliad , Programming iOS 9 begins in the middle of the story, with the reader jumping with all four feet into views and view controllers, and with a knowledge of the language and the Xcode IDE already presupposed. Discussion of the 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, memory management, and so on) material that constituted Chapters 113 in the early editions of this book, but whose presence was eventually deemed to be making the book unwieldy in size and scope has now been relegated to iOS 9 Programming Fundamentals with Swift .
So if something appears to be missing from this book, thats why! If you start reading Programming iOS 9 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 9 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.
The Scope of This Book
To a large extent, iOS 9 represents a pause in the steady march of revisions, often quite radical, that Apple has felt free to introduce with every major system update since iOS 3. In my opinion, this is all to the good. iOS 9, for the most part, is not terribly different from iOS 8. Instead, it consists mostly of fairly small but valuable rationalizations of iOS 8, giving the systems growth and development a chance to catch up with itself. For example:
- iOS 9 introduces a new notation for describing a layout constraint.
- iOS 9 expands the power of UIKit dynamics.
- iOS 9 implements unwind segues in such a way that they actually work.
In addition, many small bugs are fixed; these fixes do nothing for those who want their apps to run under iOS 8, but they are welcome nonetheless. To cite just one instance, UIProgressView custom progress images, which stopped working in iOS 7, are working once again in iOS 9.
iOS 9 also brings some important linguistic improvements. A major issue that had to be worked around in the previous edition of this book, covering iOS 8 and Swift 1.2, was that not every C and Objective-C API could be called from Swift, necessitating the frequent use of Objective-C helper methods. But in iOS 9 and Swift 2.0, this is no longer the case; Im happy to say that no Objective-C code appears in this edition.
iOS 9 does introduce a few grand sweeping innovations, such as App Transport Security and iPad multitasking; I am less happy with these, but of course I do document them nonetheless.
Heres a summary of this books major sections:
- 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 remarkable and important feature 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. This part of the book talks about all the ways in which view controllers can be manipulated in order to make their views appear. It also describes every kind of view provided by the Cocoa framework the built-in building blocks with which youll construct an apps interface.
- surveys the most important secondary 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, mail, maps, and the devices sensors.
- wraps up the book with some miscellaneous but important topics: files, networking, threading, and how to implement undo.
- summarizes the most important lifetime event messages sent to your app delegate.
- catalogs some useful Swift utility functions. You should keep an eye on this appendix, consulting it whenever a mysterious method name appears. For instance, my example code frequently uses my
delay
function, which embraces dispatch_after
with a convenient shorthand; when I use delay
and you dont know what it is, consult this appendix.
Someone who has read this book (and who, it goes without saying, is conversant with the material in iOS 9 Programming Fundamentals with Swift ) will, I believe, be capable of writing a real-life iOS app, with a clear understanding of what he or she is doing and where the app is going as it grows and develops. The book itself doesnt show how to write any particularly interesting iOS apps; but it is backed by dozens of example projects that you can download from my GitHub site, http://github.com/mattneub/Programming-iOS-Book-Examples, and it constantly uses my own real apps and real programming situations to illustrate and motivate its explanations.
Just as important, this book is intended to prepare you for your own further explorations. In the case of some topics, especially in Parts , I guide you past the initial barrier of no knowledge to reach an understanding of the topic, its concepts, its capabilities, and its documentation, along with some code examples; but the topic itself may be so huge that there is room only to introduce it here. Your feet, nevertheless, will now be set firmly on the path, and you will know enough that you can now proceed further down that path on your own whenever the need or interest arises.