Molly K. Maskrey

Beginning iPhone Development with Swift 4

Exploring the iOS SDK

4th ed.

Molly K. Maskrey

Parker, Colorado, USA

Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ .

ISBN 978-1-4842-3071-8

e-ISBN 978-1-4842-3072-5

https://doi.org/10.1007/978-1-4842-3072-5

Library of Congress Control Number: 2017957132

© Molly K. Maskrey 2017

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springer.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

Another year, another revision of this book, as well as one of my own personal tribe.

Still around after more than two years of ups and downs, KP stuck beside me during my most difficult times. No matter what I did, her sanity and silliness kept me going. She was, for a long while, my muse…my inspiration to write and to keep it as fun as possible…never taking myself too seriously. Now, as our paths slightly diverge, the qualities she’s left permeate my days and make me a better self.

Mellie has been my rock. We’ve been in and out of being friends, and I hope she knows I consider her the closest friend I’ve ever had. I probably wouldn’t be doing this if it weren’t for the love she’s shown me.

Tonya came into my life recently and has been such a godsend to help me become more confident in all I say and do, and I can’t thank her enough.

She probably has no clue she’s in here, but Lauren kept me going for the past year. When I would’ve preferred to lay in the dark and sleep and zone out, without any aggressiveness Lauren would give me something in her words to keep to going…to not give up. She—I won’t say forced, but she was very persuasive—got me into a program that is, this very day, putting me onto a better path for myself and my future and the future of anyone alongside me for my journey.

Finally, to Jennifer…our lives have radically changed over the past year. But, as partners in business and advocates for each other’s happy life, our relationship bond, while admittedly very tumultuous, has solidified into something we both see as pretty damn good.

In my personal struggles over the preceding year, these friends kept me from falling into an abyss so deep I might never have returned. Writing can be a lonely thing, and having a support system such as these beautiful, wonderful women are the only reason this endeavor was a success. A special friend told me last year that some friends are only in your life for a season. I pray that these women are friends for a lifetime.

—MM, August 2017

Acknowledgments

First, I want to acknowledge all my friends who gave me the support to persevere and go through with writing when it would have been so easy to just give up. Thanks to Sam especially, another writer I met more than a year ago, who was always there to provide support, drink tequila, and suggest fun things to do in order to keep it real. He was my cohost at almost a year of rooftop parties at Galvanize.

Thanks to Children’s Hospital Colorado and the Center for Gait and Movement Analysis, both of which have been so generous to let me be part of what they do for young adults with cerebral palsy and other gait disorders. The understanding I’ve gained drives me to focus my efforts on helping the many who truly need it. Thanks to the CEOs of 10.10.10 Health who let me see what they were doing and to provide my annoying feedback.

Thanks to the clients and friends of Global Tek Labs who so generously allowed me to include some of their projects in this book for illustrative purposes. Thanks to the hundreds of people who have attended my talks over the past year and have given me ideas for what to include, such as John Haley who told me of his personal woes in understanding the Auto Layout feature of Xcode. Those actual experiences helped drive the subject matter I chose to include in this book.

Finally, I want to acknowledge all the authors before me who set the stage for my own little work to fit into a much broader landscape.

Contents

  1. Chapter 1:​ Getting to Know the iOS Landscape
    1. About the Book
    2. Things You’ll Need
      1. Your Options as a Developer
      2. Things You Should Know
      3. Some Unique Aspects About Working in iOS
    3. What’s in This Book
      1. What’s New in This Update?​
      2. Swift and Xcode Versions
    4. Let’s Get Started
  2. Chapter 2:​ Writing Your First App
    1. Creating the Hello World Project
      1. Taking a Look at the Xcode Project Window
      2. Taking a Closer Look at the Hello World Project
    2. Introducing Xcode’s Interface Builder
      1. Introducing File Formats
      2. Exploring the Storyboard
      3. Exploring the Utilities Area
      4. Adding a Label to the View
      5. Changing Attributes
    3. Adding the Finishing Touches
    4. Exploring the Launch Screen
    5. Running the Application on a Device
    6. Summary
  3. Chapter 3:​ Basic User Interactions
    1. Understanding the MVC Paradigm
    2. Creating the ButtonFun App
    3. Understanding the ViewController
      1. Understanding Outlets and Actions
      2. Simplifying the View Controller
      3. Designing the User Interface
      4. Testing the ButtonFun App
      5. Previewing Layout
      6. Changing the Text Style
    4. Examining the Application Delegate
    5. Summary
  4. Chapter 4:​ Adding Intermediate-Level User Interactions
    1. Understanding Active, Static, and Passive Controls
    2. Creating the ControlFun Application
    3. Implementing the Image View and Text Fields
      1. Adding the Image View
      2. Resizing the Image View
      3. Setting View Attributes
      4. Using the Mode Attribute
      5. Using the Semantic Attribute
      6. Using Tag
      7. Using Interaction Check Boxes
      8. Using the Alpha Value
      9. Using Background
      10. Using Tint
      11. Drawing Check Boxes
      12. Stretching
      13. Adding the Text Fields
      14. Using Text Field Inspector Settings
      15. Setting the Attributes for the Second Text Field
      16. Adding Constraints
      17. Creating and Connecting Outlets
      18. Closing the Keyboard
      19. Closing the Keyboard When Done Is Tapped
      20. Touching the Background to Close the Keyboard
      21. Adding the Slider and Label
      22. Creating and Connecting the Actions and Outlets
      23. Implementing the Action Method
      24. Implementing the Switches, Button, and Segmented Control
      25. Adding Two Labeled Switches
      26. Connecting and Creating Outlets and Actions
      27. Implementing the Switch Actions
      28. Adding the Button
      29. Adding an Image to the Button
      30. Using Stretchable Images
      31. Using Control States
      32. Connecting and Creating the Button Outlets and Actions
      33. Implementing the Segmented Control Action
      34. Implementing the Action Sheet and Alert
      35. Displaying an Action Sheet
      36. Presenting an Alert
    4. Summary
  5. Chapter 5:​ Working with Device Rotations
    1. Understanding the Mechanics of Rotation
      1. Understanding Points, Pixels, and the Retina Display
      2. Handling Rotation
    2. Creating Your Orientations Project
      1. Understanding Supported Orientations at the App Level
      2. Understanding Per-Controller Rotation Support
    3. Creating Your Layout Project
      1. Overriding Default Constraints
      2. Using Full-Width Labels
    4. Creating Adaptive Layouts
      1. Creating the Restructure Application
      2. Setting the iPhone Landscape (wC hC) Configuration
      3. Setting the iPad (iPhone Plus Landscape) (wR hR) Configurations
    5. Summary
  6. Chapter 6:​ Creating a Multiview Application
    1. Looking at Common Types of Multiview Apps
    2. Looking at the Architecture of a Multiview Application
      1. Understanding the Root Controller
      2. Content View Anatomy
    3. Creating the View Switcher Application
      1. Renaming the View Controller
      2. Adding the Content View Controllers
      3. Modifying SwitchingViewCon​troller.​swift
      4. Building a View with a Toolbar
      5. Linking the Toolbar Button to the View Controller
      6. Writing the Root View Controller Implementation
      7. Implementing the Content Views
      8. Animating the Transition
    4. Summary
  7. Chapter 7:​ Using 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. Initial Simulator Test
      4. Implementing the Date Picker
    4. Implementing the Single-Component Picker
      1. Building the View
      2. Implementing the Data Source and Delegate
    5. Implementing a Multicomponent Picker
      1. Building the View
      2. Implementing the Controller
      3. Implementing Dependent Components
    6. Creating a Simple Game with a Custom Picker
      1. Preparing the View Controller
      2. Building the View
      3. Implementing the Controller
      4. Additional Details for Your Game
    7. Summary
  8. Chapter 8:​ Introducing Table Views
    1. Understanding Table View Basics
      1. Using Table Views and Table View Cells
      2. Understanding Grouped and Plain Tables
    2. Implementing a Simple Table
      1. Designing the View
      2. Implementing 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
    4. Implementing a Custom Table Views Application
      1. Creating a UITableViewCell Subclass
      2. Loading a UITableViewCell from a XIB File
    5. Using Grouped and Indexed Sections
      1. Building the View
      2. Importing the Data
      3. Implementing the Controller
      4. Adding an Index
      5. Adding a Search Bar
      6. Using View Debugging
    6. Summary
  9. Chapter 9:​ Adding Navigation Controllers to Table Views
    1. Understanding Navigation Controller Basics
      1. Using Stacks
      2. Using a Stack of Controllers
    2. Fonts:​ Creating a Simple Font Browser
      1. Seeing the Subcontrollers of the Fonts App
      2. Seeing the Fonts Application’s Skeleton
      3. Creating the Root View Controller
      4. Doing the Initial Storyboard Setup
      5. First Subcontroller:​ Creating the Font List View
      6. Creating the Font List Storyboard
    3. Creating the Font Sizes View Controller
      1. Creating the Font Sizes View Controller Storyboard
      2. Implementing the Font Sizes View Controller Prepare for Segue
      3. Creating the Font Info View Controller
      4. Creating the Font Info View Controller Storyboard
      5. Adapting the Font List View Controller for Multiple Segues
      6. Creating My Favorite Fonts
      7. Adding Features
      8. Implementing Swipe-to-Delete
      9. Implementing Drag-to-Reorder
    4. Summary
  10. Chapter 10:​ Collection Views
    1. Creating the DialogViewer Project
      1. Defining Custom Cells
      2. Configuring the View Controller
      3. Providing Content Cells
      4. Creating the Layout Flow
      5. Implementing the Header Views
    2. Summary
  11. Chapter 11:​ Split Views and Popovers for iPad Apps
    1. Building Master-Detail Applications with UISplitViewContr​oller
      1. Understanding How the Storyboard Defines the Structure
      2. Understanding How Code Defines the Functionality
      3. Understanding How the Master-Detail Template Application Works
      4. Adding the President Data
      5. Creating Your Own Popover
    2. Summary
  12. Chapter 12:​ App Customization with Settings and Defaults
    1. Exploring the Settings Bundle
    2. Creating the Bridge Control Application
      1. Creating the Bridge Control Project
      2. Working with the Settings Bundle
      3. Reading Settings in Your Application
      4. Changing Defaults from Your Application
      5. Registering Default Values
      6. Keeping It Real
      7. Switching to the Settings Application
    3. Summary
  13. Chapter 13:​ Persistence:​ Saving Data Between App Launches
    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. Creating the First Version of a 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
      6. Creating or Opening the Database
      7. Using Bind Variables
    5. Creating the SQLite3 Application
      1. Linking to the SQLite3 Library
    6. Using Core Data
      1. Entities and Managed Objects
      2. The Core Data Application
      3. Modifying the AppDelegate.​swift File
    7. Summary
  14. Chapter 14:​ Graphics and Drawing
    1. Quartz 2D
    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. Creating the QuartzFun Application
      2. Adding Quartz 2D Drawing Code
      3. Optimizing the QuartzFun Application
    4. Summary
  15. Appendix A: An Introduction to Swift
  16. Swift Basics
  17. Playgrounds, Comments, Variables, and Constants
  18. Predefined Types, Operators, and Control Statements
  19. Arrays, Ranges, and Dictionaries
  20. Optionals
  21. Control Statements
  22. Functions and Closures
  23. Error Handling
  24. Classes and Structures
  25. Structures
  26. Classes
  27. Properties
  28. Methods
  29. Optional Chaining
  30. Subclassing and Inheritance
  31. Protocols
  32. Extensions
  33. Summary
  34. Index

About the Author and About the Technical Reviewer

About the Author

A329781_4_En_BookFrontmatter_Figb_HTML.jpg

Molly K. Maskrey started as an electrical engineer in her 20s working for various large aerospace companies including IBM Federal Systems, TRW (now Northrup-Grumman), Loral Systems, Lockheed-Martin, and Boeing. After successfully navigating the first dot-com boom, she realized that a break was in order and took several years off, moving to Maui and teaching windsurfing at the beautiful Kanaha Beach Park.

She moved back to Colorado in 2005 and, with Jennifer, formed Global Tek Labs, an iOS development and accessory design services company that is now one of the leading consulting services for new designers looking to create smart attachments to Apple devices.

In 2014 Molly and Jennifer formed Quantitative Bioanalytics Laboratories, a wholly owned subsidiary of Global Tek to bring high-resolution mobile sensor technology to physical therapy, elder balance and fall prevention, sports performance quantification, and instrumented gait analysis (IGA). In a pivot, Molly changed the direction of QB Labs to a platform-based predictive analytics company seeking to democratize data science for smaller companies.

Molly’s background includes advanced degrees in electrical engineering, applied mathematics, data science, and business development. Molly generally speaks at a large number of conferences throughout the year including the Open Data Science Conference (ODSC) 2017 West, advancing the topic of moving analytics from the cloud to the fog for smart city initiatives. What fuels her to succeed is the opportunity to bring justice and equality to everyone whether it’s addressing food insecurity with her business partner or looking at options for better management of mental health using empirical data and tools such as natural language processing, speech pattern recognition using neural networks, or perfusion analysis in brain physiology.

About the Technical Reviewer

Bruce Wade is a software engineer from British Columbia, Canada. He started software development when he was 16 years old by coding his first web site. He went on to study computer information systems at DeVry Institute of Technology in Calgary; to further enhance his skills, he studied visual and game programming at the Art Institute of Vancouver. Over the years he has worked for large corporations as well as several startups. His software experience has led him to utilize many different technologies including C/C++, Python, Objective-C, Swift, Postgres, and JavaScript. In 2012 he started the company Warply Designed to focus on mobile 2D/3D and OS X development. Aside from hacking out new ideas, he enjoys spending time hiking with his Boxer Rasco, working out, and exploring new adventures.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.163.91