Preface

Swift 4 is an incremental release of Swift: a tock in Apple’s usual tick-tock release cycle, where the tick is a major release and the tock is a follow-up release with bug fixes and some additions and improvements. Two of the biggest additions to Swift 4 are the Decodable and Encodable protocols, which together form the Codable protocol. These protocols allow you, with just a bit of configuration, to define, encode, and decode JSON objects, a task that you either previously did on your own or used a library to accomplish.

Another welcome addition to Swift is the ability to extend typed arrays. For instance, you can now add a function to arrays that contain Int instances, but not to any other arrays. This way you could, for instance, define a sum() function that returns the sum of all integers in the array. Such a function wouldn’t make sense for an array of String objects, and therefore it’s best placed inside an extension to arrays that contain numeric data, such as an array of Int.

We all have had moments when we wished Xcode’s source editor would be rewritten in order to properly function, and that’s exactly what Apple has done with Xcode 9. We have a whole new state-of-the-art source editor with built-in GitHub integration. Xcode Server is also now built into Xcode itself, and you don’t have to install macOS server in order to take advantage of it. We have a whole chapter dedicated to Xcode Server, so you will get to know it better soon.

The previous edition of this book focused primarily on what’s new in iOS 10, but this version takes a more holistic look at iOS 11 and what can make a great iOS developer, including knowledge of continuous integration, snapshot testing, Swift 4, and Xcode 9.

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 intended audience 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 4 and will teach you some of the concepts, but if you don’t know Swift, this is not the right place to start. If you’re just starting out, pick up Apple’s book on Swift programming first; once you’ve read through it and are a bit more comfortable with Swift, come back to this book and I’m sure you’ll learn a lot of new things, even about Swift 4.

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, Continuous Integration and Delivery

Continuous integration (CI) and continuous delivery (CD) are both popular and the subject of some very deep discussions at the organizational level of development teams, sometimes with some developers being entirely against them and others enthusiastically for them. If you are lucky, you will work in an organization where everyone in the team has some knowledge of CI and CD, but if you are unlucky, you might be the only person in your team who is responsible for your CI servers. In this chapter, we will dive deep into Travis and Xcode Server as our CI servers, and also have a look at fastlane as our CD tool.

Chapter 2, Snapshot Testing

Snapshot testing is a modern way of testing your user interface in order to detect sudden and unwanted changes, such as unwanted changes to a font that can affect certain UI components. With snapshot testing, we will capture the state of our UI when we are sure of it being correct, and use this captured state as a reference to compare future states with. Should anything have changed compared to the original and correct state, the snapshot test will fail, triggering the development team to look for the bug. In this chapter, we will focus on Facebook’s snapshot testing iOS library and how we can leverage its power to make UI testing easier than ever before.

Chapter 3, SiriKit

Since its introduction, Siri has been an integral part of iOS and how people interact with the operating system. However, because it was a closed technology, we developers couldn’t integrate our apps into Siri. That’s not the case anymore. Now you can write your own app extensions that integrate into Siri and allow you to interpret various “intents” that come from Siri into your applications. For instance, you can create a financial application that allows the user to send and receive money from various sources, all driven through Siri. In this chapter, you will see how to create one of these extensions and learn the different entry points from Siri into your application.

Chapter 4, Measurements and Units

This chapter is dedicated to the new series of classes and structures that Apple has provided to developers to convert between various measurements and units.

Chapter 5, Core Data

Core Data is without a doubt the standard and best way to store large amounts of data and structure your data object models in an iOS application. In this chapter we will focus on the fundamentals of Core Data and how you can leverage its power to not only store your data, but also search for it using queries and retrieve it as managed objects.

Chapter 6, Swift and Cocoa Touch

Swift 4 brought with it a lot of great new features, such as built-in JSON parsing and typed array extensions. In this chapter we will have a look at what’s new in Swift 4 and some functionalities introduced in Cocoa Touch.

Chapter 7, Xcode 9 and Interface Builder

In this chapter, we take a look at a lot of new stuff in Xcode and Interface Builder (IB). Apple has brought us refactoring features for Swift in Xcode 9. We can also debug our iOS applications wirelessly on iOS devices, so you can finally build that iOS test farm that you have been dreaming about for years. We will look at some of these cool new features, plus GitHub integration right inside Xcode.

Chapter 8, The User Interface

Apple tends not to release a piece of technology until it knows that the implementation is really well thought out. One of the features lacking from iOS since the beginning was a built-in drag-and-drop functionality that would have allowed intra-app drag and drop on iPads, or even inter-app drag-and-drop functionality on iPhone devices. As of this year, we can now implement drag and drop in our iOS apps, thanks to the new SDK. In this chapter, you will learn about 3D Touch, drag-and-drop features, font scaling, named colors, and much more.

Chapter 9, Document-Based Apps

We’ve had the ability to develop apps that take advantage of iCloud data storage for a while now, but never have developers been able to present an iCloud document browser to their users, allowing them to create documents right in iCloud from within this system UI. We now have a view controller that functions as a built-in document browser that is easy to use in and integrate with our application. User can now open and create iCloud documents without ever leaving your applications. In this chapter you will learn all about the document browser view controller and how you can harness its power to create document-based applications.

Chapter 10, Apple Watch

This year Apple didn’t focus as much on watchOS. However, there are exciting new ways of interacting with watchOS, which we will discuss in this chapter.

Chapter 11, Contacts

The contacts APIs will be discussed in this chapter. You’ll learn how to use the Contacts framework 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—all without having to fiddle with low-level C APIs.

Chapter 12, Extensions

The Safari Content Blocker extension allows developers to create apps that get installed as extensions in the user’s Safari browser and allow us to block various elements of web pages that the user views. For instance, you can now block pictures or other 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 these extension points that you can add to your apps.

Chapter 13, Web and Search

Apps can provide content to iOS for indexing in the device’s search engine. iOS will then index this content and allow the users to search for it right within Spotlight on their devices. Your content 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 14, Multitasking

In iOS, we have the ability to provide Picture in Picture (PiP) to our users. 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 15, Maps and Location

With new additions to the Core Location and MapKit frameworks, you can, for example, display an ETA for transit between two locations or display your custom view inside the annotation of a pin on the map. You will also learn how to cluster similar pins into a group to avoid cluttering your map view. Another welcome addition to the map view is the ability to remove system buttons from it and instead display those system buttons somewhere else on your user interface, where it fits your UI best. You will learn all about that and more in this chapter.

Chapter 16, UI Testing

We will discuss Apple’s UI Testing framework in this chapter. I’ll show you how to write native Swift code to do UI testing.

Chapter 17, Core Motion

Core Motion is also available on watchOS. In this chapter, you’ll learn some of the things that you can do with this framework, including reading cadence information from sensors on the device.

Chapter 18, Security

When a user registers for your service on your website on their iOS device and saves the password with which she registered her account, this password ends up in the user’s iCloud Keychain, if she has enabled it. In your iOS app, you now have the ability to ask iOS for these credentials should the user be able to authenticate herself in your app with Touch ID. Application Transport Security in iOS forces all requests to go through HTTPS. If you build your project with the latest Xcode and iOS SDK, all your network requests will go through HTTPS by default, protecting your content—but possibly breaking a few things if you don’t support HTTPS in your web services. Read this chapter to learn more.

Chapter 19, Multimedia

iOS includes great ways for apps to interact with Siri’s voice, and you can read about them in this chapter.

Chapter 20, UI Dynamics

There are some amazing effects that you can achieve in your user interface with UI dynamics, including the ability to create turbulence or magnetic fields. In this chapter, we’ll review some examples that show these effects in action.

Chapter 21, iMessage Stickers and Apps

Sticker packs are extensions that you can distribute either as part of your iOS applications or as standalone applications. They allow you to add interactions to messages being sent and received in iMessage conversations. In this chapter, we will discuss different types of these extensions and how you can create interactive sticker pack applications for iMessage.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Note

This element signifies a note, tip, or suggestion.

Warning

This element indicates a warning or caution.

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. You can find Apple’s documentation on Swift by doing a quick web search. You can read it in your browser, as a PDF, or via iBooks.

Also 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-11-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 11 Swift Programming Cookbook by Vandad Nahavandipoor (O’Reilly). Copyright 2018 Vandad Nahavandipoor, 978-1-491-99247-0.”

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

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/ios-11-swift-prog-cookbook.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

You can also check the author’s blog and YouTube channel.

Acknowledgments

Thank you to:

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 makes 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 craftily worked his way through the book, making it even more understandable for readers. I would not have been able to write this book without Andy’s help.

Get iOS 11 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.