Preface

When I started writing this edition of this book (which has been in the field and regularly updated since iOS version 4), I thought to myself: what I should do to really overhaul it and make it even more exciting than the previous editions? The answer was to rewrite the whole book—every single word from scratch. Up to and including the iOS 8 Swift Programming Cookbook (where I made the big switch from Objective-C to Swift) was to base every edition on the previous edition. I would go through all the examples from the previous edition to remove the ones that were irrelevant for the new edition or just expendable because they were very simple and basic. I’d then update the remaining examples to make sure they work great with the new iOS version. Then I would also write new content for the new version of iOS. However, after 5 editions, I noticed that many recipes were not really useful anymore and had stayed almost the same since the beginning. So how could I solve this issue? The solution was to not do this any longer. I had to write all new content, and that’s what I did for this book.

The amount of work that went into writing this book was tremendous, on my side and on O’Reilly’s side. I have had to think long and hard about every new recipe, writing all the example code anew and ensuring it works with the latest production tools from Apple. You will learn a lot about Swift 2 and iOS 9 SDK in this book.

And yes, there is a great deal of watchOS material in this book. The chapter dedicated to watchOS has more content in it than any other chapter. The reason behind that decision was that watchOS was the main focus for this year’s WWDC and that Apple has paid more attention to watchOS in iOS 9 SDK than to other frameworks or tools that they have published this year. I hope you’ll enjoy writing watchOS apps as much as I do.

One last thing. Since Swift changes a lot and is constantly updated, please always check the GitHub repo for this book to get the most up-to-date code. I have ensured that all code compiles and works fine with Xcode 7 and the iOS 9 SDK, but if for some reason you are on a beta Xcode that is newer than the production version of Xcode 7 that Apple has released, do ensure that you have the latest code by checking out this book’s GitHub repo.

Have fun and I hope you’ll enjoy reading this book.

Audience

I assume that you are comfortable writing iOS apps, at least know your way around Xcode, and can work with the simulator. This book is not for beginners. If you have never programmed in Xcode before for iOS, it will be tough to learn iOS programming only from this book. So I suggest that you complement your skills with other online resources. The best market for this book is intermediate and advanced users.

I also assume that you have written a little bit of Swift code. In this book, I use Swift 2 and will teach you some of the concepts, but if you don’t know Swift, this is not the right place to start. Please pick up Apple’s book on Swift programming first; try with that and once you are a bit comfortable with Swift, come back to this book and I’m sure you’ll learn a lot of new things, even about Swift 2.

Organization of This Book

Here I’ll explain what each chapter is about so that you’ll get a feeling for what this book is going to teach you:

Chapter 1, Swift 2.0, Xcode 7, and Interface Builder
In this chapter, we go through a lot of new stuff in Swift, Xcode, and Interface Builder (IB), such as the addition of the guard keyword to Swift and conditionally extending types with Swift’s new runtime features. Swift has really matured with Swift 2, and I want to share some of the most important additions with you.
Chapter 2, Apple Watch
This year’s WWDC star, without a doubt, is watchOS 2 with all its additions. Now apps can run natively on the watchOS without having to talk to the iOS counterpart app, and this is great for us. More work, more things to do, more fun. We will talk about complications, transferring files between iOS and watchOS 2 apps, downloading files right on the watch, recording audio, and playing multimedia on your watch.
Chapter 3, The User Interface
Even though additions to UIKit were not talked about as extensively as watchOS 2 at this year’s WWDC, there are still tons of new features that we can discuss, including anchored constraints, stack views, and the new Safari view controller.
Chapter 4, Contacts
The all-new contacts APIs will be discussed in this chapter. The frameworks on which this chapter is based are both completely new to iOS 9. With the APIs in these frameworks, you’ll learn how to add new contacts to the user’s device, remove contacts, edit them, or even allow the user to pick a contact from the list so that you can perform your tasks on it.
Chapter 5, Extensions
Safari Content Blockers shocked a lot of developers during this year’s WWDC. This allows us developers to create apps that get installed as extensions on the user’s Safari browser, and allows us to block various elements of web pages that the user views. For instance, you can now block pictures or various unwanted elements in the websites that you specify in your app, and you can share these content blockers with those who use your app. This chapter is all about new extension points that you can add to your apps.
Chapter 6, Web and Search
Apps can now provide content to iOS. iOS will then index these contents and allow the user to search for these contents right within Spotlight on their devices. Your contents can also be indexed globally on Apple’s servers so even those who don’t have your app can see your content on their devices. Intrigued? Read this chapter, then!
Chapter 7, Multitasking
We now have Picture in Picture (PiP) in iOS. Your app can provide a video player to iOS and allow the user to minimize your whole app into that video player while she works with other apps. It’s really cool, in my opinion.
Chapter 8, Maps and Location
With the additions to Core Location and MapKit frameworks, you can now, for example, display an ETA for transit between two locations or display your custom view inside the annotation of a pin on the map.
Chapter 9, UI Testing
One of the stars of this year’s WWDC is Apple’s new UI Testing framework. We can now write native Swift code to do our UI testing, and in this chapter I’m going to show you how.
Chapter 10, Core Motion
Core Motion is now also available on watchOS 2 and in this chapter you’ll learn some of the new things that you can do with this framework, including reading cadence information from sensors on the device.
Chapter 11, Security
ATS is a welcome addition in iOS that forces all requests to go through HTTPS. If you build your project with Xcode 7 and iOS 9 SDK, all your network requests will go through HTTPS by default, protecting your content and possibly breaking a few things if you don’t support HTTPS in your web services. Read this chapter to learn more.
Chapter 12, Multimedia
We have some new additions to how apps can interact with Siri and you can read about them in this chapter.
Chapter 13, UI Dynamics
Last but not least, there are some amazing effects that you can achieve in your user interface with the new additions to UI Dynamics, including the ability to create turbulence or magnetic fields. In this chapter, I’ll show you these additions with examples.

Additional Resources

This book is not for beginners, so I assume you have already gotten a grip on Swift and can do basic things with it. Please read Apple’s documentation on Swift by doing a quick web search. You can either read it on your browser, as a PDF, or you can read it in iBooks.

Also please check this book’s GitHub repository in order to get the most up-to-date code, as I update the code to ensure it works with the latest Swift and Xcode versions.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/vandadnp/iOS-9-Swift-Programming-Cookbook.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “iOS 9 Swift Programming Cookbook by Vandad Nahavandipoor (O’Reilly). Copyright 2016 Vandad Nahavandippor, 978-1-491-93669-6.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Acknowledgments

Thank you to:

Sara, Julian, and Molly
For continuously supporting and encouraging me.
Rachel Roumeliotis
For always having trust in me and knowing that I stick to my words when I promise to write a whole new book in a short period of time with quality material. Your trust means a lot to me and I hope this book will make you proud, as much as it made me.
Andy Oram
The editor that anybody would dream about, Andy has been by my side editing this book nonstop since I started. His relentless efforts have allowed me to relax while he craftfully works his way through the book, making it even more understandable for the readers. I would not have been able to write this book without Andy’s help.
Niklas Saers
For his detailed technical review of this book.
Nicole Shelby
For all her work in getting this book ready for production. It’s been a pleasure working with you, Nicole.

Get iOS 9 Swift Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.