O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beginning iPhone Development with Swift 2: Exploring the iOS SDK

Book Description

The team that brought you the bestselling Beginning iPhone Development, the book that taught the world how to program on the iPhone, is back again for Beginning iPhone Development with Swift 2.This is the definitive guide to the Swift programming language and the iOS 9 SDK, and the source code has been updated to reflect Xcode 7 and Swift 2.

There’s coverage of brand-new technologies, as well as significant updates to existing material. You'll have everything you need to create your very own apps for the latest iOS devices. Every single sample app in the book has been rebuilt from scratch using the latest Xcode and the latest 64-bit iOS 9-specific project templates, and designed to take advantage of the latest Xcode features.

Assuming little or no working knowledge of the new Swift programming language, and written in a friendly, easy-to-follow style, this book offers a complete soup-to-nuts course in iPhone, iPad, and iPod touch programming. The book starts with the basics, walking through the process of downloading and installing Xcode and the iOS 9 SDK, and then guides you though the creation of your first simple application.

From there, 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. 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 iPhone 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!

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Authors
  8. About the Technical Reviewer
  9. Chapter 1: Welcome to the Swift Jungle
    1. What This Book Is
    2. What You Need
      1. Developer Options
      2. What You Need to Know
    3. What’s Different About Coding for iOS?
      1. Only One Active Application
      2. Only One Window
      3. Limited Access
      4. Limited Response Time
      5. Limited Screen Size
      6. Limited System Resources
      7. Some New Stuff
      8. A Different Approach
    4. What’s in This Book
    5. What’s New in This Update?
    6. Swift and Xcode Versions
    7. Are You Ready?
  10. Chapter 2: Appeasing the Tiki Gods
    1. Setting Up Your Project in Xcode
      1. The Xcode Project Window
      2. A Closer Look at Our Project
    2. Introducing Xcode’s Interface Builder
      1. File Formats
      2. The Storyboard
      3. The Utilities Area
      4. Adding a Label to the View
      5. Changing Attributes
    3. Some iPhone Polish: Finishing Touches
    4. The Launch Screen
    5. Running the Application on a Device
    6. Bring It on Home
  11. Chapter 3: Handling Basic Interaction
    1. The Model-View-Controller Paradigm
    2. Creating Our Project
    3. Looking at the View Controller
      1. Understanding Outlets and Actions
      2. Cleaning Up the View Controller
      3. Designing the User Interface
      4. Trying It Out
      5. Previewing Layout
      6. Adding Some style
    4. Looking at the Application Delegate
    5. Bring It on Home
  12. Chapter 4: More User Interface Fun
    1. A Screen Full of Controls
    2. Active, Static, and Passive Controls
    3. Creating the Application
    4. Implementing the Image View and Text Fields
      1. Adding the Image View
      2. Resizing the Image View
      3. Setting View Attributes
      4. Adding the Text Fields
      5. Adding Constraints
      6. Creating and Connecting Outlets
    5. Closing the Keyboard
      1. Closing the Keyboard When Done Is Tapped
      2. Touching the Background to Close the Keyboard
      3. Adding the Slider and Label
      4. Creating and Connecting the Actions and Outlets
      5. Implementing the Action Method
    6. Implementing the Switches, Button, and Segmented Control
      1. Implementing the Switch Actions
    7. Spiffing Up the Button
      1. Stretchable Images
      2. Control States
      3. Connecting and Creating the Button Outlets and Actions
    8. Implementing the Segmented Control Action
    9. Implementing the Action Sheet and Alert
      1. Showing an Action Sheet
      2. Showing an Alert
    10. Crossing the Finish Line
  13. Chapter 5: Rotation and Adaptive Layout
    1. The Mechanics of Rotation
      1. Points, Pixels, and the Retina Display
      2. Handling Rotation
    2. Choosing Your View Orientations
      1. Supported Orientations at the App Level
      2. Per-Controller Rotation Support
    3. Designing an Interface Using Constraints
      1. Overriding Default Constraints
      2. Full-Width Labels
    4. Creating Adaptive Layouts
      1. The Restructure Application
      2. Size Classes
      3. Size Classes and Storyboards
      4. Creating the iPhone Landscape Layout
      5. Adding the iPad Layout
    5. Rotating Out of Here
  14. Chapter 6: Multiview Applications
    1. Common Types of Multiview Apps
    2. The Architecture of a Multiview Application
      1. The Root Controller
      2. Anatomy of a Content View
    3. Building View Switcher
      1. Renaming the View Controller
      2. Adding the Content View Controllers
      3. Modifying SwitchingViewController.swift
      4. Building a View with a Toolbar
      5. Linking the Toolbar Button to the View Controller
      6. Writing the Root View Controller
      7. Implementing the Content Views
      8. Animating the Transition
    4. Switching Off
  15. Chapter 7: Tab Bars and Pickers
    1. The Pickers Application
    2. Delegates and Data Sources
    3. Creating the Pickers Application
      1. Creating the View Controllers
      2. Creating the Tab Bar Controller
      3. The Initial Test Run
    4. Implementing the Date Picker
    5. Implementing the Single-Component Picker
      1. Building the View
      2. Implementing the Controller As a Data Source and Delegate
    6. Implementing a Multicomponent Picker
      1. Building the View
      2. Implementing the Controller
    7. Implementing Dependent Components
    8. Creating a Simple Game with a Custom Picker
      1. Preparing the View Controller
      2. Building the View
      3. Implementing the Controller
      4. Final Details
    9. Final Spin
  16. Chapter 8: Introduction to Table Views
    1. Table View Basics
      1. Table Views and Table View Cells
      2. Grouped and Plain Tables
    2. Implementing a Simple Table
      1. Designing the View
      2. Writing the Controller
      3. Adding an Image
      4. Using Table View Cell Styles
      5. Setting the Indent Level
      6. Handling Row Selection
      7. Changing the Font Size and Row Height
    3. Customizing Table View Cells
      1. Adding Subviews to the Table View Cell
      2. Creating a UITableViewCell Subclass
      3. Loading a UITableViewCell from a Nib
    4. Grouped and Indexed Sections
      1. Building the View
      2. Importing the Data
      3. Implementing the Controller
      4. Adding an Index
    5. Implementing a Search Bar
    6. How Many Tables?: View Debugging
    7. Putting It All on the Table
  17. Chapter 9: Navigation Controllers and Table Views
    1. Navigation Controller Basics
      1. Stacky Goodness
      2. A Stack of Controllers
    2. Fonts: A Simple Font Browser
      1. Meet the Subcontrollers
      2. The Fonts Application’s Skeleton
    3. Creating the Root View Controller
    4. Initial Storyboard Setup
    5. First Subcontroller: The Font List View
      1. Storyboarding the Font List
      2. Making the Root View Controller Prepare for Segues
    6. Creating the Font Sizes View Controller
      1. Storyboarding the Font Sizes View Controller
      2. Making the Font List View Controller Prepare for Segues
    7. Creating the Font Info View Controller
      1. Storyboarding the Font Info View Controller
      2. Setting Up Constraints
      3. Adapting the Font List View Controller for Multiple Segues
      4. My Favorite Fonts
    8. Table View Niceties
      1. Implementing Swipe-to-Delete
      2. Implementing Drag-to-Reorder
    9. Breaking the Tape
  18. Chapter 10: Collection View and Stack View
    1. Creating the DialogViewer Project
    2. Defining Custom Cells
    3. Configuring the View Controller
    4. Providing Content Cells
    5. Making the Layout Flow
    6. Providing Header Views
    7. Working with UIStackView
      1. Creating a Vertical Stack
      2. Adding a Horizontal Stack
      3. Completing the Application
  19. Chapter 11: Using Split Views and Popovers
    1. Building Master-Detail Applications with UISplitViewController
      1. The Storyboard Defines the Structure
      2. The Code Defines the Functionality
      3. How the Master-Detail Template Application Works
    2. Here Come the Presidents
    3. Creating Your Own Popover
    4. Split Views on the iPhone
      1. Split Views on the iPhone 6/6s Plus
      2. Getting the iPhone 6/6s Plus Behavior on All iPhones
    5. Customizing the Split View
    6. Time to Wrap Up and Split
  20. Chapter 12: Application Settings and User Defaults
    1. Getting to Know Your Settings Bundle
    2. The Bridge Control Application
      1. Creating the Project
      2. Working with the Settings Bundle
      3. Reading Settings in Our Application
      4. Changing Defaults from Our Application
      5. Registering Default Values
      6. Keeping It Real
    3. Switching to the Settings Application
    4. Beam Me Up, Scotty
  21. Chapter 13: Basic Data Persistence
    1. Your Application’s Sandbox
      1. Getting the Documents and Library Directories
      2. Getting the tmp Directory
    2. File-Saving Strategies
      1. Single-File Persistence
      2. Multiple-File Persistence
    3. Using Property Lists
      1. Property List Serialization
      2. The First Version of the Persistence Application
    4. Archiving Model Objects
      1. Conforming to NSCoding
      2. Implementing NSCopying
      3. Archiving and Unarchiving Data Objects
      4. The Archiving Application
    5. Using iOS’s Embedded SQLite3
      1. Creating or Opening the Database
      2. Using Bind Variables
      3. The SQLite3 Application
    6. Using Core Data
      1. Entities and Managed Objects
      2. The Core Data Application
    7. Persistence Rewarded
  22. Chapter 14: Documents and iCloud
    1. Managing Document Storage with UIDocument
      1. Building TinyPix
      2. Creating TinyPixDocument
      3. Code Master
      4. Initial Storyboarding
      5. Creating TinyPixView
      6. Storyboard Detailing
    2. Adding iCloud Support
      1. Creating a Provisioning Profile
      2. How to Query
      3. Save Where?
      4. Storing Preferences on iCloud
    3. What We Didn’t Cover
  23. Chapter 15: Grand Central Dispatch, Background Processing, and You
    1. Grand Central Dispatch
    2. Introducing SlowWorker
    3. Threading Basics
    4. Units of Work
    5. GCD: Low-Level Queuing
      1. Improving SlowWorker
    6. Background Processing
      1. Application Life Cycle
      2. State-Change Notifications
      3. Creating State Lab
      4. Exploring Execution States
      5. Using Execution State Changes
      6. Handling the Inactive State
      7. Handling the Background State
    7. Grand Central Dispatch, Over and Out
  24. Chapter 16: Drawing with Core Graphics
    1. Paint the World
    2. The Quartz 2D Approach to Drawing
      1. Quartz 2D’s Graphics Contexts
      2. The Coordinate System
      3. Specifying Colors
      4. Drawing Images in Context
      5. Drawing Shapes: Polygons, Lines, and Curves
      6. Quartz 2D Tool Sampler: Patterns, Gradients, and Dash Patterns
    3. The QuartzFun Application
      1. Setting Up the QuartzFun Application
      2. Adding Quartz 2D Drawing Code
      3. Optimizing the QuartzFun Application
    4. Drawing to a Close
  25. Chapter 17: Getting Started with SpriteKit
    1. Simple Beginnings
      1. Initial Scene Customization
    2. Player Movement
      1. Adding the Player to the Scene
      2. Handling Touches: Player Movement
      3. Geometry Calculations
      4. Wobbly Bits
    3. Creating Your Enemies
    4. Putting Enemies in the Scene
    5. Start Shooting
      1. Defining Your Physics Categories
      2. Creating the BulletNode Class
      3. Applying Physics
      4. Adding Bullets to the Scene
    6. Attacking Enemies with Physics
    7. Finishing Levels
      1. Keeping Tabs on the Enemies
      2. Transitioning to the Next Level
    8. Customizing Collisions
      1. Adding a Class Extension to SKNode
      2. Adding Custom Collision Behavior to Enemies
      3. Showing Accurate Player Lives
    9. Spicing Things Up with Particles
      1. Your First Particle
      2. Putting Particles into the Scene
    10. The End Game
    11. At Last, a Beginning: Create a StartScene
    12. A Sound Is Worth a Thousand Pictures
    13. Making the Game a Little Harder: Force Fields
    14. Game Over
  26. Chapter 18: Taps, Touches, and Gestures
    1. Multitouch Terminology
    2. The Responder Chain
      1. Responding to Events
      2. Forwarding an Event: Keeping the Responder Chain Alive
    3. The Multitouch Architecture
    4. The Four Touch Notification Methods
    5. The TouchExplorer Application
    6. The Swipes Application
      1. Using Touch Events to Detect Swipes
      2. Automatic Gesture Recognition
      3. Implementing Multiple Swipes
    7. Detecting Multiple Taps
    8. Detecting Pinch and Rotation
    9. Defining Custom Gestures
      1. The CheckPlease Application
      2. The CheckPlease Touch Methods
    10. Garçon? Check, Please!
  27. Chapter 19: Where Am I? Finding Your Way with Core Location and Map Kit
    1. The Location Manager
      1. Setting the Desired Accuracy
      2. Setting the Distance Filter
      3. Getting Permission to Use Location Services
      4. Starting the Location Manager
      5. Using the Location Manager Wisely
    2. The Location Manager Delegate
      1. Getting Location Updates
      2. Getting Latitude and Longitude Using CLLocation
      3. Error Notifications
    3. Trying Out Core Location
      1. Using Location Manager Updates
    4. Visualizing Your Movement on a Map
      1. Changing Location Service Permissions
    5. Wherever You Go, There You Are
  28. Chapter 20: Whee! Gyro and Accelerometer!
    1. Accelerometer Physics
    2. Don’t Forget Rotation
    3. Core Motion and the Motion Manager
      1. Event-Based Motion
      2. Proactive Motion Access
      3. Gyroscope and Attitude Results
      4. Accelerometer Results
    4. Detecting Shakes
      1. Baked-In Shaking
      2. Shake and Break
    5. Accelerometer As Directional Controller
      1. Rolling Marbles
      2. Writing the Ball View
      3. Calculating Ball Movement
    6. Rolling On
  29. Chapter 21: The Camera and Photo Library
    1. Using the Image Picker and UIImagePickerController
      1. Using the Image Picker Controller
      2. Implementing the Image Picker Controller Delegate
    2. Road Testing the Camera and Library
      1. Designing the Interface
      2. Implementing the Camera View Controller
    3. It’s a Snap!
  30. Chapter 22: Application Localization
    1. Localization Architecture
    2. Strings Files
      1. What’s in a Strings File?
      2. The Localized String Function
    3. Real-World iOS: Localizing Your Application
      1. Setting Up LocalizeMe
      2. Trying Out LocalizeMe
      3. Localizing the Project
      4. Localizing the Storyboard
      5. Generating and Localizing a Strings File
      6. Localizing the App Display Name
    4. Adding Another Localization
    5. Auf Wiedersehen
  31. Appendix A: A Swift Introduction to Swift
    1. Swift Basics
      1. Playgrounds, Comments, Variables, and Constants
      2. Predefined Types, Operators, and Control Statements
      3. Arrays, Ranges, and Dictionaries
      4. Optionals
      5. Control Statements
      6. Functions and Closures
      7. Error Handling
    2. Classes and Structures
      1. Structures
      2. Classes
      3. Properties
      4. Methods
      5. Optional Chaining
      6. Subclassing and Inheritance
      7. Protocols
      8. Extensions
    3. To Swiftly Go...
  32. Index