Skip to main content
main-content
Top

About this book

Create your very own apps for the latest iOS devices. You'll start with the basics, and then work your way through the process of downloading and installing Xcode and the iOS 10 SDK, and then guides you though the creation of your first simple application.

Assuming little or no working knowledge of the Swift programming language, and written in a friendly, easy-to-follow style, Beginning iPhone Development with Swift 3 offers a comprehensive course in iPhone and iPad programming. In this third edition of the best-selling book, you’ll learn how to integrate all the interface elements iOS users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders.

Every single sample app in the book has been rebuilt from scratch using the latest Xcode and the latest iOS 10-specific project templates, and designed to take advantage of the latest Xcode features. Discover brand-new technologies, as well as significant updates to existing tools. You’ll master a variety of design patterns, from the simplest single view to complex hierarchical drill-downs. The art of table building will be demystified, and you’ll learn how to save your data using the iOS file system. You’ll also learn how to save and retrieve your data using a variety of persistence techniques, including Core Data and SQLite. And there’s much more!

What You Will Learn

Develop your own bestselling iPhone and iPad appsUtilize Swift playgroundsDisplay data in Table Views

Draw to the screen using Core GraphicsUse iOS sensor capabilities to map your worldGet your app to work with iCloud and more

Who This Book is For

Anyone who wants to start developing for iPhone and iPad.

Table of Contents

Chapter 1. Getting to Know the iOS Landscape

Abstract
Coding for Apple mobile devices provides developers a rewarding and lucrative career path where you might not only change people’s lives with your app (see Figure 1-1), but you’ll also have a great time being with bright, like-minded women and men such as yourself.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 2. Writing Our First App

Abstract
I want to get you started right away with a feel for what this is all about and to motivate your continued progress toward being a great developer, so let’s get to it and do something with our iPhone (see Figure 2-1).
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 3. Basic User Interactions

Abstract
Our Hello World app provided a good introduction to iOS development using Xcode and Cocoa Touch, but it lacked a crucial capability—the ability to interact with the user.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 4. Adding Intermediate Level User Interactions

Abstract
In Chapter 3, we discussed MVC and built an application using it. You learned about outlets and actions, and you used them to tie a button control to a text label.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 5. Working with Device Rotations

Abstract
The iPhone and iPad exude amazing engineering in form, fit and function. Apple engineers found all kinds of ways to squeeze maximum functionality into a very small and elegant package.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 6. Creating a Multiview Application

Abstract
Up until this point, we’ve written applications using a single view controller. While single view apps can often do what you need them to, the real power of the iOS platform emerges when you switch out views based on user input. Multiview applications come in several different flavors, but the underlying mechanism functions the same, regardless of how the app appears on the screen. In this chapter, we’ll focus on the structure of multiview applications and the basics of swapping content views by building our own multiview app from scratch. Writing our own custom controller class that switches between two different content views, we’ll establishing a strong foundation for taking advantage of the various multiview controllers provided by Apple.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 7. Using Tab Bars and Pickers

Abstract
In the previous chapter, you built your first multiview application. In this chapter, we’ll build another one—this time, creating a full tab bar application with five different tabs and five different content views. Building this application reinforces a lot of what we covered in Chapter 6. We’ll use those five content views to demonstrate a type of iOS control not yet covered, a picker view, or just a picker. You may not be familiar with the name, but you’ve almost certainly used a picker if you own an iPhone or iPod touch for more than 10 minutes. Pickers contain controls with dials that spin. You use them to input dates in the Calendar application or to set a timer in the Clock application, as shown in Figure 7-1. It is not quite as common on an iPad, since the larger display lets you present other ways of choosing among multiple items, but even there, it’s used by the Calendar application.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 8. Introducing Table Views

Abstract
Over the course of the next few chapters, we’ll build some hierarchical navigation-based applications similar to the Mail application that ships on iOS devices. Applications of this type, usually called master-detail applications, allow the user to drill down into nested lists of data and edit that data. But before we can build these types of applications, we’ll need to master the concept of table views.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 9. Adding Navigation Controllers to Table Views

Abstract
In the previous chapter, we worked through the basics of using table views. In this chapter, we’ll go further by adding navigation controllers.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 10. Collection Views

Abstract
For years, iOS developers used the UITableView component to create a huge variety of interfaces. With its ability to let you define multiple cell types, create them on the fly as needed, and handily scroll them vertically, UITableView became a key component of thousands of apps. While Apple has increased the capability of table views with every major new iOS release, it’s still not the ultimate solution for all large sets of data. If you want to present data in multiple columns, for example, you need to combine all the columns for each row of data into a single cell. There’s also no way to make a UITableView scroll its content horizontally. In general, much of the power of UITableView came with a particular trade-off: developers have no control of the overall layout of a table view. You define the look of each individual cell, but the cells are just going to be stacked on top of each other in one big scrolling list.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 11. Split Views and Popoversfor iPad Apps

Abstract
In Chapter 9, we spent a lot of time dealing with app navigation based on selections in table views, where each selection causes the top-level view, which filled the entire screen, to slide left and bring in the next view in the hierarchy. Many iPhone and iPod touch apps work this way such as Mail, which lets you drill down through mail accounts and folders until you make your way to the message. Although, this approach works on the iPad, it leads to a user interaction problem.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 12. App Customization with Settings and Defaults

Abstract
All but the simplest apps you’re likely to use typically include a preferences window where the user sets application-specific options. On macOS, you’ll usually find a Preferences… menu item in the application’s menu. Selecting it brings up a window where the user enters and changes various options. The iPhone and iPad include a dedicated application called Settings that you’ve likely used before. In this chapter, we’ll show you how to add settings for your iOS application to the Settings app and how to access those settings from within your application.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 13. Persistence: Saving Data Between App Launches

Abstract
So far, we’ve focused on the controller and view aspects of the MVC paradigm. Although several of our applications read data from their own application bundle, only the Bridge Control example in Chapter 12 places any data in persistent storage. When any of our other apps launched, they appeared with exactly the same data they had when first launched. That approach worked up to this point, but in the real world, your apps need to persist data. When users make changes, they want to see those changes when they launch the program again.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 14. Documents and iCloud

Abstract
Apple’s iCloud service introduced with iOS 5 (see Figure 14-1) provides cloud storage for iOS devices, as well as for computers running macOS. Most iOS users probably encounter the iCloud device backup option immediately when setting up a new device or upgrading an old device to a more recent version of iOS, quickly discovering the advantages of automatic backup that doesn’t require the use of a computer.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 15. Multithreaded Programming Using Grand Central Dispatch

Abstract
While the idea of programming multithreaded functions in any environment may seem daunting at first (see Figure 15-1), Apple came up with a new approach that makes multithreaded programming much easier. Grand Central Dispatch comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and macOS.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 16. Graphics and Drawing

Abstract
We’ve constructed all our application UIs so far using views and controls that are part of the UIKit framework. We can do a lot with UIKit, and a great many applications are constructed using only its predefined objects. Some visual elements (see Figure 16-1), however, can’t be fully realized without going beyond what the UIKit stock components offer.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 17. Simple Games Using SpriteKit

Abstract
In iOS 7, Apple introduced SpriteKit, a framework for the high-performance rendering of 2D graphics. Unlike Core Graphics (which is focused on drawing graphics using a painter’s model) or Core Animation (which is focused on animating attributes of GUI elements), SpriteKit focuses on a different area entirely—video games—and it is Apple’s first foray into the graphical side of game programming in the iOS era. It was released for iOS 7 and OS X 10.9 (Mavericks) at the same time, providing the same API on both platforms, so that apps written for one can be easily ported to the other. Although Apple has never before supplied a framework quite like SpriteKit, it has clear similarities to various open source libraries such as Cocos2D. If you’ve used Cocos2D or something similar in the past, you’ll feel right at home.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 18. Taps, Touches, and Gestures

Abstract
The screens of the iPhone, iPod touch, and iPad—with their crisp, bright, touch-sensitive display—represent masterpieces of engineering. The multitouch screen common to all iOS devices provides one of the key factors in the platform’s tremendous usability. Because the screen can detect multiple touches at the same time and track them independently, applications are able to detect a wide range of gestures, giving the user power that goes beyond the interface.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 19. Determining Location

Abstract
Every iOS device has the ability to determine where in the world it is, using a framework called Core Location. iOS also includes the Map Kit framework, which lets you easily create a live interactive map that shows any locations you like, including, of course, the user’s location. In this chapter, we’ll work with both of these frameworks. Core Location can actually leverage three technologies to do this: GPS, cell ID location, and Wi-Fi Positioning Service (WPS). GPS provides the most accurate positioning of the three technologies, but it is not available on first-generation iPhones, iPod touches, or Wi-Fi-only iPads. In short, any device with at least a 3G data connection also contains a GPS unit. GPS reads microwave signals from multiple satellites to determine the current location.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 20. Device Orientation and Motion

Abstract
The iPhone, iPad, and iPod touch all include a built-in accelerometer—the tiny device that lets iOS know how the device is being held and if it’s being moved. iOS uses the accelerometer to handle autorotation, and many games use it as a control mechanism. The accelerometer can also be used to detect shakes and other sudden movement. This capability was extended even further with the introduction of the iPhone 4, which was the first iPhone to include a built-in gyroscope to let developers determine the angle at which the device is positioned around each axis. The gyro and accelerometer are now standard on all new iPads and iPod touches. In this chapter, we’ll explore using the Core Motion framework to access the gyro and accelerometer values in your application.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 21. Using the Camera and Accessing Photos

Abstract
It should come as no surprise that the iPhone, iPad, and iPod touch contain a built-in camera with the Photos application to help you manage the pictures and videos you’ve taken (see Figure 21-1). What you may not know is that your programs can use the built-in camera to take pictures. Your applications can also allow the user to select from among and view the media already stored on the device. We’ll look at both of these abilities in this chapter.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Chapter 22. Translating Apps Using Localization

Abstract
At the time of this writing, you’ll find iOS devices available in about half the world’s countries, with that number continuing to increase over time (see Figure 22-1). Available on every continent except Antarctica, the iPad and iPod touch continue to sell all over the world and are nearly as ubiquitous as the iPhone. If you plan on releasing applications through the App Store, think about more than just people in your own country who speak your own language. Fortunately, iOS provides a robust localization architecture letting you easily translate your application (or have it translated by others) into, not only multiple languages, but even into multiple dialects of the same language. Providing different terminology to English speakers in the United Kingdom as opposed to the United States no longer represents a problem.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche

Appendix A. An Introduction to Swift

Abstract
Until recently, writing an iPhone or iPad application meant working with Objective-C. Because of its unusual syntax, Objective-C is one of the most polarizing of programming languages—people tend to love it or hate it. At the World Wide Developer Conference in 2014, Apple changed all that by unveiling an alternative—a new language called Swift. Swift’s syntax is designed to be easily recognizable to programmers who are used to some of the more popular object-oriented programming languages like C++ and Java, therefore making it easier for them to start writing applications for iOS (and for Macs, since Swift is also fully supported as a development language on macOS). This appendix covers the parts of Swift that you’ll need to know in order to understand the example code in this book. We assume that you already have some programming experience and that you know what variables, functions, methods, and classes are. This appendix is neither a reference nor an exhaustive guide to the language—for that, there are numerous other resources, some of which are listed in Chapter 1.
Molly Maskrey, Kim Topley, David Mark, Fredrik Olsson, Jeff Lamarche
Additional information