You are previewing The Core iOS Developer’s Cookbook, Fifth Edition.
O'Reilly logo
The Core iOS Developer’s Cookbook, Fifth Edition

Book Description

The best place to start iOS application development

The Core iOS Developer’s Cookbook provides ready-to-use code solutions for common iOS challenges, providing recipes that help you master the core technology at the heart of iOS. Renowned iOS programming expert Erica Sadun and top iOS developer Rich Wardwell cover the topics you need to create successful mobile applications that take full advantage of iOS graphics, touches, and views.

As in all of Sadun’s iOS bestsellers, The Core iOS Developer’s Cookbook translates modern best practices into working code, distilling key concepts into concise recipes you can easily understand and apply to your own projects. This isn’t just cut-and-paste; using examples, Sadun and Wardwell fully explain both the “how” and “why” that underscore effective and successful iOS development.

Coverage includes

  • Creating direct touch-based interfaces with multi-touch, gestures, and custom gesture recognizers

  • Building and customizing controls in powerful ways

  • Adding novel motion effects

  • Alerting users via pop-ups, progress bars, local notifications, popovers, audio cues, and more

  • Using Xcode modules to easily integrate system frameworks and headers

  • Assembling views and animation, organizing view hierarchies, and understanding how views work together

  • Supporting multiple screen geometries with the breakthrough Auto Layout constraints system

  • Controlling keyboards, making onscreen elements “text aware,” and efficiently scanning and formatting text

  • Organizing user workspaces with view controllers

  • Managing photos, videos, email, and text messages

  • Leveraging enhanced iOS support for social media activities, including Flickr and Vimeo

  • Implementing VoiceOver accessibility, including text-to-speech

  • Getting started with Core Data-managed data stores

  • Leveraging powerful networking and web services support

  • Using APIs to enhance the user experience

  • Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents
    6. Preface
      1. What You’ll Need
      2. Your Roadmap to Mac/iOS Development
      3. How This Book Is Organized
      4. About the Sample Code
      5. Getting the Sample Code
      6. Contribute!
      7. Getting Git
      8. Getting GitHub
      9. Contacting the Authors
    7. Acknowledgments
      1. Endnote
    8. About the Authors
    9. Editor’s Note: We Want to Hear from You!
    10. Reader Services
    11. 1. Gestures and Touches
      1. Touches
        1. Phases
        2. Touches and Responder Methods
        3. Touching Views
        4. Multi-Touch
        5. Gesture Recognizers
      2. Recipe: Adding a Simple Direct Manipulation Interface
      3. Recipe: Adding Pan Gesture Recognizers
      4. Recipe: Using Multiple Gesture Recognizers Simultaneously
        1. Resolving Gesture Conflicts
      5. Recipe: Constraining Movement
      6. Recipe: Testing Touches
      7. Recipe: Testing Against a Bitmap
      8. Recipe: Drawing Touches Onscreen
      9. Recipe: Smoothing Drawings
      10. Recipe: Using Multi-Touch Interaction
      11. Recipe: Detecting Circles
      12. Recipe: Creating a Custom Gesture Recognizer
      13. Recipe: Dragging from a Scroll View
      14. Recipe: Live Touch Feedback
        1. Enabling Touch Feedback
        2. Intercepting and Forwarding Touch Events
        3. Implementing the TOUCHkit Overlay View
      15. Recipe: Adding Menus to Views
      16. Summary
    12. 2. Building and Using Controls
      1. The UIControl Class
        1. Target-Action
        2. Kinds of Controls
        3. Control Events
      2. Buttons
      3. Buttons in Interface Builder
        1. Connecting Buttons to Actions
      4. Recipe: Building Buttons
        1. Multiline Button Text
        2. Adding Animated Elements to Buttons
        3. Adding Extra State to Buttons
      5. Recipe: Animating Button Responses
      6. Recipe: Adding a Slider with a Custom Thumb
        1. Customizing UISlider
        2. Adding Efficiency
      7. Recipe: Creating a Twice-Tappable Segmented Control
        1. Second-Tap Feedback
        2. Controls and Attributes
      8. Working with Switches and Steppers
      9. Recipe: Subclassing UIControl
        1. Creating Controls
        2. Tracking Touches
        3. Dispatching Events
      10. Recipe: Building a Star Slider
      11. Recipe: Building a Touch Wheel
      12. Recipe: Creating a Pull Control
        1. Discoverability
        2. Testing Touches
      13. Recipe: Building a Custom Lock Control
        1. Adding a Page Indicator Control
      14. Recipe: Image Gallery Viewer
      15. Building Toolbars
      16. Summary
    13. 3. Alerting the User
      1. Talking Directly to Your User through Alerts
        1. Building Simple Alerts
        2. Alert Delegates
        3. Displaying the Alert
        4. Kinds of Alerts
      2. Recipe: Using Blocks with Alerts
        1. Blocks
        2. Retain Cycles and Blocks
      3. Recipe: Using Variadic Arguments with Alert Views
      4. Presenting Lists of Options
        1. Scrolling Menus
        2. Displaying Text in Action Sheets
      5. “Please Wait”: Showing Progress to Your User
        1. Using UIActivityIndicatorView
        2. Using UIProgressView
      6. Recipe: Modal Progress Overlays
        1. Tappable Overlays
      7. Recipe: Custom Modal Alert View
        1. Frosted Glass Effect
      8. Recipe: Basic Popovers
      9. Recipe: Local Notifications
        1. Best Practices
      10. Alert Indicators
        1. Badging Applications
      11. Recipe: Simple Audio Alerts
        1. System Sounds
        2. Modules for System Frameworks
        3. Vibration
        4. Alerts
        5. Delays
        6. Disposing of System Sounds
      12. Summary
    14. 4. Assembling Views and Animations
      1. View Hierarchies
      2. Recipe: Recovering a View Hierarchy Tree
        1. Exploring XIB and Storyboard Views
      3. Recipe: Querying Subviews
      4. Managing Subviews
        1. Adding Subviews
        2. Reordering and Removing Subviews
        3. View Callbacks
      5. Tagging and Retrieving Views
        1. Using Tags to Find Views
      6. Recipe: Naming Views by Object Association
        1. Naming Views in Interface Builder
      7. View Geometry
        1. Frames
        2. Rectangle Utility Functions
        3. Points and Sizes
        4. Transforms
        5. Coordinate Systems
      8. Recipe: Working with View Frames
        1. Adjusting Sizes
        2. CGRects and Centers
        3. Other Geometric Elements
      9. Recipe: Retrieving Transform Information
        1. Retrieving Transform Properties
        2. Testing for View Intersection
      10. Display and Interaction Traits
      11. UIView Animations
        1. Building Animations with Blocks
      12. Recipe: Fading a View In and Out
      13. Recipe: Swapping Views
      14. Recipe: Flipping Views
      15. Recipe: Using Core Animation Transitions
      16. Recipe: Bouncing Views as They Appear
      17. Recipe: Key Frame Animations
      18. Recipe: Image View Animations
      19. Summary
    15. 5. View Constraints
      1. What Are Constraints?
      2. Constraint Attributes
        1. Constraint Math
      3. The Laws of Constraints
      4. Constraints and Frames
        1. Intrinsic Content Size
        2. Alignment Rectangles
      5. Creating Constraints
        1. Basic Constraint Declarations
        2. Visual Format Constraints
        3. Variable Bindings
      6. Format Strings
        1. Orientation
        2. Connections
      7. Predicates
        1. Metrics
        2. View-to-View Predicates
        3. Priorities
      8. Format String Summary
      9. Aligning Views and Flexible Sizing
      10. Constraint Processing
      11. Managing Constraints
      12. Recipe: Comparing Constraints
        1. Animating Constraints
      13. Recipe: Creating Fixed-Size Constrained Views
        1. Disabling Autoresizing Constraints
        2. Starting Within View Bounds
        3. Constraining Size
        4. Putting It Together
      14. Recipe: Centering Views
      15. Recipe: Setting Aspect Ratio
      16. Recipe: Responding to Orientation Changes
      17. Debugging Your Constraints
      18. Recipe: Describing Constraints
      19. Constraint Macros
        1. Macros
      20. Summary
    16. 6. Text Entry
      1. Recipe: Dismissing a UITextField Keyboard
        1. Preventing Keyboard Dismissal
        2. Text Trait Properties
        3. Other Text Field Properties
      2. Recipe: Dismissing Text Views with Custom Accessory Views
      3. Recipe: Adjusting Views Around Keyboards
      4. Recipe: Creating a Custom Input View
      5. Recipe: Making Text-Input-Aware Views
      6. Recipe: Adding Custom Input Views to Nontext Views
        1. Adding Input Clicks
      7. Recipe: Building a Better Text Editor (Part I)
      8. Recipe: Building a Better Text Editor (Part II)
        1. Enabling Attributed Text
        2. Controlling Attributes
        3. Other Responder Functionality
      9. Recipe: Text-Entry Filtering
      10. Recipe: Detecting Text Patterns
        1. Rolling Your Own Expressions
        2. Enumerating Regular Expressions
        3. Data Detectors
        4. Using Built-in Type Detectors
        5. Useful Websites
      11. Recipe: Detecting Misspelling in a UITextView
        1. Spell Checker Protocol
      12. Searching for Text Strings
      13. Summary
    17. 7. Working with View Controllers
      1. View Controllers
        1. The UIViewController Class
        2. Navigation Controllers
        3. Tab Bar Controllers
        4. Split View Controllers
        5. Page View Controllers
        6. Popover Controllers
      2. Developing with Navigation Controllers and Split Views
        1. Using Navigation Controllers and Stacks
        2. Pushing and Popping View Controllers
        3. Bar Buttons
        4. Edge-to-Edge Layout
      3. Recipe: The Navigation Item Class
        1. Titles and Back Buttons
        2. Macros
      4. Recipe: Modal Presentation
        1. Presenting a Custom Modal Information View
      5. Recipe: Building Split View Controllers
      6. Recipe: Creating Universal Split View/Navigation Apps
      7. Recipe: Tab Bars
      8. Remembering Tab State
      9. Recipe: Page View Controllers
        1. Book Properties
        2. Wrapping the Implementation
        3. Exploring the Recipe
        4. Building a Presentation Index
      10. Recipe: Custom Containers
        1. Adding and Removing a Child View Controller
        2. Transitioning Between View Controllers
      11. Recipe: Segues
        1. Segues and IB
      12. Summary
    18. 8. Common Controllers
      1. Image Picker Controller
        1. Image Sources
        2. Presenting the Picker on iPhone and iPad
      2. Recipe: Selecting Images
        1. How To: Adding Photos to the Simulator
        2. The Assets Library Module
        3. Presenting a Picker
        4. Handling Delegate Callbacks
      3. Recipe: Snapping Photos
        1. Setting Up the Picker
        2. Displaying Images
        3. Saving Images to the Photo Album
      4. Recipe: Recording Video
        1. Creating the Video-Recording Picker
        2. Saving the Video
      5. Recipe: Playing Video with Media Player
      6. Recipe: Editing Video
        1. AV Foundation and Core Media
      7. Recipe: Picking and Editing Video
      8. Recipe: E-mailing Pictures
        1. Creating Message Contents
      9. Recipe: Sending a Text Message
      10. Recipe: Posting Social Updates
        1. Data Sharing and Viewing
      11. Summary
    19. 9. Creating and Managing Table Views
      1. iOS Tables
      2. Delegation
      3. Creating Tables
        1. Table Styles
        2. Laying Out the View
        3. Assigning a Data Source
        4. Serving Cells
        5. Registering Cell Classes
        6. Dequeuing Cells
        7. Assigning a Delegate
      4. Recipe: Implementing a Basic Table
        1. Data Source Methods
        2. Responding to User Touches
      5. Table View Cells
        1. Selection Style
        2. Adding Custom Selection Traits
      6. Recipe: Creating Checked Table Cells
      7. Working with Disclosure Accessories
      8. Recipe: Table Edits
        1. Adding Undo Support
        2. Implementing Undo
        3. Displaying Remove Controls
        4. Handling Delete Requests
        5. Swiping Cells
        6. Reordering Cells
        7. Adding Cells
      9. Recipe: Working with Sections
        1. Building Sections
        2. Counting Sections and Rows
        3. Returning Cells
        4. Creating Header Titles
        5. Customizing Headers and Footers
        6. Creating a Section Index
        7. Handling Section Mismatches
        8. Delegation with Sections
      10. Recipe: Searching Through a Table
        1. Creating a Search Display Controller
        2. Registering Cells for the Search Display Controller
        3. Building Searchable Data Source Methods
        4. Delegate Methods
        5. Using a Search-Aware Index
      11. Recipe: Adding Pull-to-Refresh to Your Table
      12. Recipe: Adding Action Rows
      13. Coding a Custom Group Table
        1. Creating Grouped Preferences Tables
      14. Recipe: Building a Multiwheel Table
        1. Creating the UIPickerView
        2. Data Source and Delegate Methods
        3. Using Views with Pickers
      15. Using UIDatePicker
        1. Creating the Date Picker
      16. Summary
    20. 10. Collection Views
      1. Collection Views Versus Tables
        1. Practical Implementation Differences
      2. Establishing Collection Views
        1. Controllers
        2. Views
        3. Data Sources and Delegates
      3. Flow Layouts
        1. Scroll Direction
        2. Item Size and Line Spacing
        3. Header and Footer Sizing
        4. Insets
      4. Recipe: Basic Collection View Flows
      5. Recipe: Custom Cells
      6. Recipe: Scrolling Horizontal Lists
      7. Recipe: Introducing Interactive Layout Effects
      8. Recipe: Scroll Snapping
      9. Recipe: Creating a Circle Layout
        1. Creation and Deletion Animation
        2. Powering the Circle Layout
        3. The Layout
      10. Recipe: Adding Gestures to Layout
      11. Recipe: Creating a True Grid Layout
      12. Recipe: Custom Item Menus
        1. Double-Tap Alternative
      13. Summary
    21. 11. Documents and Data Sharing
      1. Recipe: Working with Uniform Type Identifiers
        1. Determining UTIs from File Extensions
        2. Moving from UTI to Extension or MIME Type
        3. Testing Conformance
        4. Retrieving Conformance Lists
      2. Recipe: Accessing the System Pasteboard
        1. Storing Data
        2. Storing Common Types
        3. Retrieving Data
        4. Passively Updating the Pasteboard
      3. Recipe: Monitoring the Documents Folder
        1. Enabling Document File Sharing
        2. User Control
        3. Xcode Access
        4. Scanning for New Documents
      4. Recipe: Activity View Controller
        1. Presenting the Activity View Controller
        2. Activity Item Sources
        3. Item Providers
        4. Item Source Callbacks
        5. Adding Services
        6. Items and Activities
        7. Excluding Activities
      5. Recipe: The Quick Look Preview Controller
        1. Implementing Quick Look
      6. Recipe: Using the Document Interaction Controller
        1. Creating Document Interaction Controller Instances
        2. Document Interaction Controller Properties
        3. Providing Document Quick Look Support
        4. Checking for the Open Menu
      7. Recipe: Declaring Document Support
        1. Creating Custom Document Types
        2. Implementing Document Support
      8. Recipe: Creating URL-Based Services
        1. Declaring the Scheme
        2. Testing URLs
        3. Adding the Handler Method
      9. Summary
    22. 12. A Taste of Core Data
      1. Introducing Core Data
      2. Entities and Models
        1. Building a Model File
        2. Attributes and Relationships
        3. Building Object Classes
      3. Creating Contexts
      4. Adding Data
        1. Examining the Data File
      5. Querying the Database
        1. Setting Up the Fetch Request
        2. Performing the Fetch
      6. Removing Objects
      7. Recipe: Using Core Data for a Table Data Source
        1. Index Path Access
        2. Section Key Path
        3. Section Groups
        4. Index Titles
        5. Table Readiness
      8. Recipe: Search Tables and Core Data
      9. Recipe: Adding Edits to Core Data Table Views
        1. Adding Undo/Redo Support
        2. Creating Undo Transactions
        3. Rethinking Edits
      10. Recipe: A Core Data–Powered Collection View
      11. Summary
    23. 13. Networking Basics
      1. Recipe: Checking Your Network Status
      2. Scanning for Connectivity Changes
      3. The URL Loading System
        1. Configuration
        2. Tasks
        3. NSURLSession
      4. Recipe: Simple Downloads
      5. Recipe: Downloads with Feedback
        1. Resuming Downloads
      6. Recipe: Background Transfers
        1. Testing Background Transfers
        2. Web Services
      7. Recipe: Using JSON Serialization
      8. Recipe: Converting XML into Trees
        1. Trees
        2. Building a Parse Tree
      9. Summary
    24. 14. Device-Specific Development
      1. Accessing Basic Device Information
      2. Adding Device Capability Restrictions
        1. User Permission Descriptions
        2. Other Common Info.plist Keys
      3. Recipe: Checking Device Proximity and Battery States
        1. Enabling and Disabling the Proximity Sensor
        2. Monitoring the Battery State
        3. Detecting Retina Support
      4. Recipe: Recovering Additional Device Information
      5. Core Motion Basics
        1. Testing for Sensors
        2. Accessing Sensor Data
      6. Recipe: Using Acceleration to Locate “Up”
      7. Working with Basic Orientation
        1. Calculating Orientation from the Accelerometer
        2. Calculating a Relative Angle
      8. Recipe: Using Acceleration to Move Onscreen Objects
        1. Setup and Teardown
      9. Recipe: Accelerometer-Based Scroll View
      10. Recipe: Retrieving and Using Device Attitude
      11. Detecting Shakes Using Motion Events
      12. Recipe: Using External Screens
        1. Detecting Screens
        2. Retrieving Screen Resolutions
        3. Setting Up Video Out
        4. Adding a Display Link
        5. Overscanning Compensation
        6. VIDEOkit
      13. Tracking Users
      14. One More Thing: Checking for Available Disk Space
      15. Summary
    25. 15. Accessibility
      1. Accessibility 101
        1. Accessibility in IB
      2. Enabling Accessibility
      3. Traits
      4. Labels
      5. Hints
      6. Testing with the Simulator
      7. Broadcasting Updates
      8. Testing Accessibility on iOS
      9. Speech Synthesis
      10. Dynamic Type
      11. Summary
    26. A. Objective-C Literals
      1. Numbers
      2. Boxing
        1. Enums
      3. Container Literals
      4. Subscripting
      5. Feature Tests
    27. Index