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

Programming iOS 12

Book Description

If you’re grounded in the basics of Swift, Xcode, and the Cocoa framework, this book provides a structured explanation of all essential real-world iOS app components. Through deep exploration and copious code examples, you’ll learn how to create views, manipulate view controllers, and add features from iOS frameworks.

  • Create, arrange, draw, layer, and animate views that respond to touch
  • Use view controllers to manage multiple screens of interface
  • Master interface classes for scroll views, table views, text, popovers, split views, web views, and controls
  • Dive into frameworks for sound, video, maps, and sensors
  • Access user libraries: music, photos, contacts, and calendar
  • Explore additional topics, including files, networking, and threads

Stay up-to-date on iOS 12 innovations, such as User Notification framework improvements, as well as changes in Xcode 10 and Swift 4.2. All example code is available on GitHub for you to download, study, and run.

Want to brush up on the basics? Pick up iOS 12 Programming Fundamentals with Swift to learn about Swift, Xcode, and Cocoa. Together with Programming iOS 12, you’ll gain a solid, rigorous, and practical understanding of iOS 12 development.

Table of Contents

  1. Preface
    1. The Scope of This Book
    2. Versions
    3. Acknowledgments
    4. From the Programming iOS 4 Preface
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Safari
    8. How to Contact Us
  2. I. Views
    1. 1. Views
      1. The Window and Root View
        1. How an App Launches
        2. Subclassing UIWindow
        3. Referring to the Window
      2. Experimenting with Views
      3. Subview and Superview
      4. Visibility and Opacity
      5. Frame
      6. Bounds and Center
      7. Transform
      8. The View Environment
        1. Window Coordinates and Screen Coordinates
        2. Trait Collections and Size Classes
      9. Layout
        1. Autoresizing
        2. Autolayout and Constraints
        3. Implicit Autoresizing Constraints
        4. Creating Constraints in Code
        5. Constraints as Objects
        6. Margins and Guides
        7. Intrinsic Content Size and Alignment Rects
        8. Stack Views
        9. Internationalization
        10. Mistakes with Constraints
      10. Configuring Layout in the Nib
        1. Autoresizing in the Nib
        2. Creating a Constraint
        3. Viewing and Editing Constraints
        4. Problems with Nib Constraints
        5. Varying the Screen Size
        6. Conditional Interface Design
      11. Xcode View Features
        1. View Debugger
        2. Previewing Your Interface
        3. Designable Views and Inspectable Properties
      12. Layout Events
        1. Layout-Driven UI
        2. Tweaking Autolayout
        3. Simulating Autolayout
    2. 2. Drawing
      1. Images and Image Views
        1. Image Files
        2. Image Views
        3. Resizable Images
        4. Transparency Masks
        5. Reversible Images
      2. Graphics Contexts
        1. Drawing On Demand
        2. Drawing a UIImage
      3. UIImage Drawing
      4. CGImage Drawing
      5. Snapshots
      6. CIFilter and CIImage
      7. Blur and Vibrancy Views
      8. Drawing a UIView
      9. Graphics Context Commands
        1. Graphics Context Settings
        2. Paths and Shapes
        3. Clipping
        4. Gradients
        5. Colors and Patterns
        6. Graphics Context Transforms
        7. Shadows
        8. Erasing
      10. Points and Pixels
      11. Content Mode
    3. 3. Layers
      1. View and Layer
      2. Layers and Sublayers
        1. Manipulating the Layer Hierarchy
        2. Positioning a Sublayer
        3. CAScrollLayer
      3. Layer and Delegate
      4. Layout of Layers
      5. Drawing in a Layer
        1. Drawing-Related Layer Properties
        2. Content Resizing and Positioning
        3. Layers that Draw Themselves
      6. Transforms
        1. Affine Transforms
        2. 3D Transforms
        3. Depth
      7. Further Layer Features
        1. Shadows
        2. Borders and Rounded Corners
        3. Masks
      8. Layer Efficiency
      9. Layers and Key–Value Coding
    4. 4. Animation
      1. Drawing, Animation, and Threading
      2. Image View and Image Animation
      3. View Animation
        1. A Brief History of View Animation
        2. Property Animator Basics
        3. View Animation Basics
        4. View Animation Configuration
        5. Timing Curves
        6. Canceling a View Animation
        7. Frozen View Animation
        8. Custom Animatable View Properties
        9. Keyframe View Animation
        10. Transitions
      4. Implicit Layer Animation
        1. Animation Transactions
        2. Media Timing Functions
      5. Core Animation
        1. CABasicAnimation and Its Inheritance
        2. Using a CABasicAnimation
        3. Springing Animation
        4. Keyframe Animation
        5. Making a Property Animatable
        6. Grouped Animations
        7. Freezing an Animation
        8. Transitions
        9. Animations List
      6. Actions
        1. What an Action Is
        2. Action Search
        3. Hooking Into the Action Search
        4. Making a Custom Property Implicitly Animatable
        5. Nonproperty Actions
      7. Emitter Layers
      8. CIFilter Transitions
      9. UIKit Dynamics
        1. The Dynamics Stack
        2. Custom Behaviors
        3. Animator and Behaviors
      10. Motion Effects
      11. Animation and Layout
    5. 5. Touches
      1. Touch Events and Views
      2. Receiving Touches
      3. Restricting Touches
      4. Interpreting Touches
      5. Gesture Recognizers
        1. Gesture Recognizer Classes
        2. Gesture Recognizer Conflicts
        3. Subclassing Gesture Recognizers
        4. Gesture Recognizer Delegate
        5. Gesture Recognizers in the Nib
      6. 3D Touch Press Gesture
      7. Touch Delivery
      8. Hit-Testing
        1. Performing Hit-Testing
        2. Hit-Test Munging
        3. Hit-Testing For Layers
        4. Hit-Testing For Drawings
        5. Hit-Testing During Animation
      9. Initial Touch Event Delivery
      10. Gesture Recognizer and View
      11. Touch Exclusion Logic
      12. Gesture Recognition Logic
  3. II. Interface
    1. 6. View Controllers
      1. View Controller Responsibilities
      2. View Controller Hierarchy
        1. Automatic Child View Placement
        2. Manual Child View Placement
        3. Presentation View Placement
        4. Ensuring a Coherent Hierarchy
      3. View Controller Creation
      4. How a View Controller Obtains Its View
        1. Manual View
        2. Generic Automatic View
        3. View in a Separate Nib
        4. Summary
      5. How Storyboards Work
        1. How a View Controller Nib is Loaded
        2. How a View Nib is Loaded
      6. View Resizing
        1. View Size in the Nib Editor
        2. Bars and Underlapping
        3. Resizing and Layout Events
      7. Rotation
        1. Detecting Rotation
        2. Uses of Rotation
        3. Permitting Compensatory Rotation
        4. Initial Orientation
      8. View Controller Manual Layout
        1. Initial Manual Layout
        2. Manual Layout During Rotation
      9. Presented View Controller
        1. Presentation and Dismissal
        2. Configuring a Presentation
        3. Communication with a Presented View Controller
        4. Adaptive Presentation
        5. Presentation, Rotation, and the Status Bar
      10. Tab Bar Controller
        1. Tab Bar Items
        2. Configuring a Tab Bar Controller
      11. Navigation Controller
        1. Bar Button Items
        2. Navigation Items and Toolbar Items
        3. Configuring a Navigation Controller
      12. Custom Transition
        1. Noninteractive Custom Transition Animation
        2. Interactive Custom Transition Animation
        3. Custom Presented View Controller Transition
        4. Transition Coordinator
      13. Page View Controller
        1. Preparing a Page View Controller
        2. Page View Controller Navigation
        3. Other Page View Controller Configurations
      14. Container View Controllers
        1. Adding and Removing Children
        2. Status Bar, Traits, and Resizing
      15. Peek and Pop
      16. Storyboards
        1. Triggered Segues
        2. Container Views and Embed Segues
        3. Storyboard References
        4. Unwind Segues
      17. View Controller Lifetime Events
        1. Order of Events
        2. Appear and Disappear Events
        3. Event Forwarding to a Child View Controller
      18. View Controller Memory Management
        1. Lazy Loading
        2. NSCache, NSPurgeableData, and Memory-Mapping
        3. Background Memory Usage
      19. State Restoration
        1. How to Test State Restoration
        2. Participating in State Restoration
        3. Restoration ID, Identifier Path, and Restoration Class
        4. Restoring View Controller State
        5. Restoration Order of Operations
        6. Restoration of Other Objects
    2. 7. Scroll Views
      1. Content Size
      2. Creating a Scroll View in Code
        1. Manual Content Size
        2. Automatic Content Size with Autolayout
        3. Scroll View Layout Guides
        4. Using a Content View
      3. Scroll View in a Nib
        1. No Internal Autolayout
        2. Internal Autolayout
      4. Content Inset
      5. Scrolling
        1. Scrolling in Code
        2. Paging
        3. Tiling
      6. Zooming
        1. Zooming Programmatically
        2. Zooming with Detail
      7. Scroll View Delegate
      8. Scroll View Touches
      9. Floating Scroll View Subviews
      10. Scroll View Performance
    3. 8. Table Views and Collection Views
      1. Table View Controller
      2. Table View Cells
        1. Built-In Cell Styles
        2. Registering a Cell Class
        3. Custom Cells
      3. Table View Data
        1. The Three Big Questions
        2. Reusing Cells
      4. Table View Sections
        1. Section Headers and Footers
        2. Section Index
      5. Refreshing a Table View
        1. Direct Access to Cells
        2. Refresh Control
      6. Variable Row Heights
        1. Manual Row Height Measurement
        2. Measurement and Layout with Constraints
        3. Estimated Height
        4. Automatic Row Height
      7. Table View Selection
        1. Managing Cell Selection
        2. Responding to Cell Selection
        3. Navigation from a Table View
        4. Cell Choice and Static Tables
      8. Table View Scrolling and Layout
      9. Table View State Restoration
      10. Table View Searching
        1. Configuring a Search Controller
        2. Using a Search Controller
      11. Table View Editing
        1. Toggling a Table View’s Edit Mode
        2. Changing a Table View’s Structure
        3. Deleting Cells
        4. Custom Action Buttons
        5. Editable Content in Cells
        6. Inserting Cells
        7. Rearranging Cells
        8. Collapsing and Expanding Cells
      12. Table View Menus
      13. Collection Views
      14. Collection View Classes
      15. Using a Collection View
        1. Deleting Cells
        2. Rearranging Cells
      16. Custom Collection View Layouts
        1. Flow Layout Subclass
        2. Collection View Layout Subclass
        3. Decoration Views
      17. Switching Layouts
      18. Collection Views and UIKit Dynamics
    4. 9. iPad Interface
      1. Popovers
        1. Arrow Source and Direction
        2. Popover Size
        3. Popover Appearance
        4. Passthrough Views
        5. Popover Presentation, Dismissal, and Delegate
        6. Adaptive Popovers
        7. Popover Segues
        8. Popover Presenting a View Controller
      2. Split Views
        1. Expanded Split View Controller (iPad)
        2. Collapsed Split View Controller (iPhone)
        3. Expanding Split View Controller (Big iPhone)
        4. Customizing a Split View Controller
        5. Split View Controller in a Storyboard
        6. Setting the Collapsed State
        7. View Controller Message Percolation
      3. iPad Multitasking
      4. Drag and Drop
        1. Drag and Drop Architecture
        2. Basic Drag and Drop
        3. Item Providers
        4. Slow Data Delivery
        5. Additional Delegate Methods
        6. Table Views and Collection Views
        7. Spring Loading
        8. iPhone and Local Drag and Drop
    5. 10. Text
      1. Fonts and Font Descriptors
        1. Fonts
        2. Font Descriptors
      2. Attributed Strings
        1. Attributed String Attributes
        2. Making an Attributed String
        3. Modifying and Querying an Attributed String
        4. Custom Attributes
        5. Drawing and Measuring an Attributed String
      3. Labels
        1. Number of Lines
        2. Wrapping and Truncation
        3. Resizing a Label to Fit Its Text
        4. Customized Label Drawing
      4. Text Fields
        1. Summoning and Dismissing the Keyboard
        2. Keyboard Covers Text Field
        3. Text Field Delegate and Control Event Messages
        4. Text Field Menu
        5. Drag and Drop
        6. Keyboard and Input Configuration
      5. Text Views
        1. Links, Text Attachments, and Data
        2. Self-Sizing Text View
        3. Text View and Keyboard
      6. Text Kit
        1. Text View and Text Kit
        2. Text Container
        3. Alternative Text Kit Stack Architectures
        4. Layout Manager
        5. Text Kit Without a Text View
    6. 11. Web Views
      1. WKWebView
        1. Web View Content
        2. Tracking Changes in a Web View
        3. Web View Navigation
        4. Communicating with a Web Page
        5. Custom Schemes
        6. Web View Peek and Pop
      2. Safari View Controller
      3. Developing Web View Content
    7. 12. Controls and Other Views
      1. UIActivityIndicatorView
      2. UIProgressView
        1. Progress View Alternatives
        2. The Progress Class
      3. UIPickerView
      4. UISearchBar
      5. UIControl
        1. UISwitch
        2. UIStepper
        3. UIPageControl
        4. UIDatePicker
        5. UISlider
        6. UISegmentedControl
        7. UIButton
        8. Custom Controls
      6. Bars
        1. Bar Position and Bar Metrics
        2. Bar Appearance
        3. UIBarButtonItem
        4. UINavigationBar
        5. UIToolbar
        6. UITabBar
      7. Tint Color
      8. Appearance Proxy
    8. 13. Modal Dialogs
      1. Alerts and Action Sheets
        1. Alerts
        2. Action Sheets
        3. Dialog Alternatives
      2. Quick Actions
      3. Local Notifications
        1. Authorization for Local Notifications
        2. Notification Categories
        3. Scheduling a Local Notification
        4. Hearing About a Local Notification
        5. Grouped Notifications
        6. Managing Notifications
        7. Notification Content Extensions
      4. Today Extensions
      5. Activity Views
        1. Presenting an Activity View
        2. Custom Activities
        3. Action Extensions
        4. Share Extensions
  4. III. Some Frameworks
    1. 14. Audio
      1. System Sounds
      2. Audio Session
        1. Category
        2. Activation and Deactivation
        3. Ducking
        4. Interruptions
        5. Secondary Audio
        6. Routing Changes
      3. Audio Player
      4. Remote Control of Your Sound
      5. Playing Sound in the Background
      6. AVAudioEngine
      7. MIDI Playback
      8. Text to Speech
      9. Speech to Text
      10. Further Topics in Sound
    2. 15. Video
      1. AVPlayerViewController
        1. Other AVPlayerViewController Properties
        2. Picture-in-Picture
      2. Introducing AV Foundation
        1. Some AV Foundation Classes
        2. Things Take Time
        3. Time is Measured Oddly
        4. Constructing Media
        5. AVPlayerLayer
        6. Further Exploration of AV Foundation
      3. UIVideoEditorController
    3. 16. Music Library
      1. Music Library Authorization
      2. Exploring the Music Library
        1. Querying the Music Library
        2. Persistence and Change in the Music Library
      3. Music Player
      4. MPVolumeView
      5. Playing Songs with AV Foundation
      6. Media Picker
    4. 17. Photo Library and Camera
      1. Browsing with UIImagePickerController
        1. Image Picker Controller Presentation
        2. Image Picker Controller Delegate
        3. Dealing with Image Picker Controller Results
      2. Photos Framework
        1. Querying the Photo Library
        2. Modifying the Library
        3. Being Notified of Changes
        4. Fetching Images
        5. Editing Images
        6. Photo Editing Extension
      3. Using the Camera
        1. Capture with UIImagePickerController
        2. Capture with AV Foundation
    5. 18. Contacts
      1. Contact Classes
      2. Fetching Contact Information
        1. Fetching a Contact
        2. Repopulating a Contact
        3. Labeled Values
        4. Contact Formatters
      3. Saving Contact Information
      4. Contact Sorting, Groups, and Containers
      5. Contacts Interface
        1. CNContactPickerViewController
        2. CNContactViewController
    6. 19. Calendar
      1. Calendar Database Contents
        1. Calendars
        2. Calendar Items
        3. Calendar Database Changes
      2. Creating Calendars, Events, and Reminders
        1. Events
        2. Alarms
        3. Recurrence
        4. Reminders
        5. Proximity Alarms
      3. Fetching Events and Reminders
      4. Calendar Interface
        1. EKEventViewController
        2. EKEventEditViewController
        3. EKCalendarChooser
    7. 20. Maps
      1. Displaying a Map
      2. Annotations
        1. Customizing an MKMarkerAnnotationView
        2. Changing the Annotation View Class
        3. Custom Annotation View Class
        4. Custom Annotation Class
        5. Annotation View Hiding and Clustering
        6. Other Annotation Features
      3. Overlays
        1. Custom Overlay Class
        2. Custom Overlay Renderer
        3. Other Overlay Features
      4. Map Kit and Current Location
      5. Communicating with the Maps App
      6. Geocoding, Searching, and Directions
        1. Geocoding
        2. Searching
        3. Directions
    8. 21. Sensors
      1. Core Location
        1. Location Manager, Delegate, and Authorization
        2. Location Tracking
        3. Where Am I?
        4. Background Location
        5. Heading
      2. Acceleration, Attitude, and Activity
        1. Shake Events
        2. Using Core Motion
        3. Raw Acceleration
        4. Gyroscope
        5. Other Core Motion Data
  5. IV. Final Topics
    1. 22. Persistent Storage
      1. The Sandbox
        1. Standard Directories
        2. Inspecting the Sandbox
        3. Basic File Operations
        4. Saving and Reading Files
      2. User Defaults
      3. Simple Sharing and Previewing of Files
        1. File Sharing
        2. Document Types and Receiving a Document
        3. Handing Over a Document
        4. Previewing a Document
        5. Quick Look Previews
      4. Document Architecture
        1. A Basic Document Example
        2. iCloud
        3. Document Browser
        4. Custom Thumbnails
        5. Custom Previews
        6. Document Picker
      5. XML
      6. JSON
      7. SQLite
      8. Core Data
      9. PDFs
      10. Image Files
    2. 23. Basic Networking
      1. HTTP Requests
        1. Obtaining a Session
        2. Session Configuration
        3. Session Tasks
        4. Session Delegate
        5. HTTP Request with Task Completion Function
        6. HTTP Request with Session Delegate
        7. One Session, One Delegate
        8. Delegate Memory Management
        9. Session and Delegate Encapsulation
        10. Downloading Table View Data
        11. Background Session
      2. On-Demand Resources
      3. In-App Purchases
    3. 24. Threads
      1. Main Thread
      2. Background Threads
      3. Why Threading Is Hard
      4. Blocking the Main Thread
      5. Manual Threading
      6. Operation
      7. Grand Central Dispatch
        1. Commonly Used GCD Methods
        2. Synchronous Execution
        3. Dispatch Groups
        4. One-time Execution
        5. Concurrent Queues
        6. Checking the Queue
      8. Threads and App Backgrounding
    4. 25. Undo
      1. Target–Action Undo
      2. Undo Grouping
      3. Functional Undo
      4. Undo Interface
        1. Shake-To-Edit
        2. Undo Menu
    5. A. Application Lifetime Events
      1. Application States
      2. App Delegate Events
      3. App Lifetime Scenarios
        1. Major State Changes
        2. Paused Inactivity
        3. Transient Inactivity on the iPad
      4. Lifetime Event Timing
    6. B. Some Useful Utility Functions
      1. Launch Without Main Storyboard
      2. Core Graphics Initializers
      3. Center of a CGRect
      4. Adjust a CGSize
      5. Delayed Performance
      6. Dictionary of Views
      7. Constraint Priority Arithmetic
      8. Constraint Issues
      9. Configure a Value Class at Point of Use
      10. Finite Repetition of an Animation
      11. Remove Multiple Indexes from an Array
    7. C. How Asynchronous Works
  6. Index