Mastering iOS 12 Programming - Third Edition

Book description

Become a professional iOS developer with the most in-depth and advanced guide to Swift, Xcode 10, ARKit, and Core ML

Key Features

  • Explore the extensive world of iOS development through practical examples
  • Gain detailed insights into core iOS programming concepts such as app extensions and performance
  • Extend your iOS apps by adding augmented reality and machine learning capabilities

Book Description

The iOS development environment has significantly matured, and with Apple users spending more money in the App Store, there are plenty of development opportunities for professional iOS developers. However, the journey to mastering iOS development and the new features of iOS 12 is not straightforward. This book will help you make that transition smoothly and easily. With the help of Swift 4.2, you'll not only learn how to program for iOS 12, but also how to write efficient, readable, and maintainable Swift code that maintains industry best practices.

Mastering iOS 12 Programming will help you build real-world applications and reflect the real-world development flow. You will also find a mix of thorough background information and practical examples, teaching you how to start implementing your newly gained knowledge.

By the end of this book, you will have got to grips with building iOS applications that harness advanced techniques and make best use of the latest and greatest features available in iOS 12.

What you will learn

  • Build a professional iOS application using Xcode 10 and Swift 4.2
  • Use AutoLayout to create complex layouts that look great on every device
  • Delve into advanced animations with UIViewPropertyAnimator and UIKit Dynamics
  • Enhance your app by using instruments and building your own profiling tools
  • Integrate iMessage, Siri, and more in your app through app extensions
  • Train and use machine learning models with Core ML 2 and Create ML
  • Create engaging augmented reality experiences with ARKit 2

Who this book is for

If you're a developer with some experience in iOS programming and want to enhance your skills by unlocking the full potential of the latest iOS version with Swift to build great applications, this book is for you.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering iOS 12 Programming Third Edition
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. UITableView Touch-up
    1. Setting up the user interface
    2. Fetching a user's contacts
      1. Creating a custom UITableViewCell to show contacts
        1. Designing a table-view cell
        2. Creating the table-view cell subclass
      2. Displaying the list of contacts
        1. Protocols and delegation
        2. Conforming to the UITableViewDataSource and UITableViewDelegate protocols
    3. Under-the-hood performance of UITableView
      1. Improving performance with prefetching
    4. UITableViewDelegate and interactions
      1. Responding to cell-selection
      2. Implementing cell-deletion
      3. Allowing the user to reorder cells
    5. Summary
    6. Questions
    7. Further reading
  8. A Better Layout with UICollectionView
    1. Converting from a table view to a collection view
      1. Creating and implementing a custom collection view cell
    2. Understanding UICollectionViewFlowLayout and its delegate
      1. Creating a custom UICollectionViewLayout
        1. Pre-calculating the layout
        2. Implementing collectionViewContentSize
        3. Implementing layoutAttributesForElements(in:)
        4. Implementing layoutAttributesForItem(at:)
        5. Implementing shouldInvalidateLayout(forBoundsChange:)
        6. Assigning a custom layout to your collection view
        7. Final words on the custom layout
    3. UICollectionView performance
    4. Implementing user interactions for the collection view
      1. Enabling cell selection for your collection view
      2. Allowing users to delete cells
        1. Reordering cells in a collection view
        2. Refactoring the long-press handler
        3. Implementing the reordering method calls
        4. Implementing the data source methods
        5. Adding the edit button
    5. Summary
    6. Questions
    7. Further reading
  9. Creating a Detail Page
    1. Building universal applications
    2. Implementing navigation with segues
    3. Creating adaptive layouts with Auto Layout
      1. Auto Layout with Interface Builder
        1. Implementing a scroll view with Auto Layout
        2. Laying out the image and name label
        3. Adjusting the image and label for large screens
        4. Laying out the bottom section
        5. Adjusting the bottom section for small screens
      2. Using Auto Layout in code
        1. Implementing the compact size layout
        2. Implementing the regular size layout
      3. Improving layouts with UIStackView
        1. Containing labels in a stack view
    4. Passing data between view controllers
      1. Updating the data loading and model
      2. Passing the model to the details page
      3. Implementing the new outlets and displaying data
    5. Enhancing the user experience with 3D Touch
    6. Summary
    7. Questions
    8. Further reading
  10. Immersing Your Users with Animation
    1. Refactoring existing animations with UIViewPropertyAnimator
    2. Understanding and controlling animation progress
    3. Adding vibrancy to animations
    4. Adding dynamism with UIKit Dynamics
    5. Customizing view-controller transitions
      1. Implementing a custom modal presentation transition
      2. Making an interactive dismissal transition
      3. Implementing a custom UINavigationController transition
    6. Summary
    7. Questions
    8. Further reading
  11. Understanding the Swift Type System
    1. Knowing what types are available
      1. Reference types
      2. Value types
        1. Understanding structs
        2. Understanding enums
    2. Understanding differences in types
      1. Comparing value types to reference types
        1. Differences in usage
        2. Differences in memory allocation
    3. Deciding which type you should use
      1. When to use a reference type?
      2. When to use a value type?
    4. Summary
    5. Questions
    6. Further reading
  12. Writing Flexible Code with Protocols and Generics
    1. Defining your own protocols
    2. Checking for traits instead of types
      1. Extending your protocols with default behavior
      2. Improving your protocols with associated types
    3. Adding flexibility with generics
    4. Summary
    5. Questions
    6. Further reading
  13. Improving the Application Structure
    1. Properly separating concerns
      1. Extracting the contact-fetching code
      2. Extracting the bounce animation
    2. Adding protocols for clarity
      1. Defining the ViewEffectAnimatorType protocol
      2. Defining a contact-displayable protocol
    3. Summary
    4. Questions
    5. Further reading
  14. Adding Core Data to Your App
    1. Understanding the Core Data Stack
    2. Adding Core Data to an existing application
    3. Creating a Core Data model
      1. Creating the models
      2. Defining relationships
      3. Using your entities
    4. Reading and writing data in a Core Data database
      1. Understanding data persistence
      2. Persisting your models
      3. Refactoring the persistence code
      4. Reading data with a simple fetch request
      5. Filtering data with predicates
      6. Reacting to database changes
        1. Implementing NSFetchedResultsController
    5. Understanding the use of multiple instances of NSManagedObjectContext
    6. Summary
    7. Questions
    8. Further reading
  15. Fetching and Displaying Data from the Network
    1. Fetching data from the web
      1. Understanding URLSession basics
    2. Working with JSON in Swift
    3. Updating Core Data objects with fetched data
      1. Implementing the fetch logic
      2. Updating a movie with a popularity rating
      3. Visualizing multiple threads
    4. Wrapping the feature up
      1. Adding the rating to the movie cell
      2. Understanding App Transport Security
      3. Observing changes to movie ratings
    5. Summary
    6. Questions
    7. Further reading
  16. Being Proactive with Background Fetch
    1. Understanding how background fetch works
      1. Looking at background fetch from a distance
      2. Looking at background fetch in more depth
    2. Implementing the prerequisites for background fetch
      1. Adding the background fetch capability
      2. Asking iOS to wake your app
    3. Updating movies in the background
      1. Updating the data model
      2. Refactoring the existing code
    4. Updating movies in the background
      1. Preparing the helper struct
      2. Updating the movies
    5. Summary
    6. Test your knowledge
    7. Further reading
  17. Syncing Data with CloudKit
    1. Getting familiar with CloudKit
      1. Adding CloudKit to your project
      2. Exploring the CloudKit dashboard
        1. Understanding your CloudKit database
        2. Exploring the rest of your container
    2. Storing and retrieving data with CloudKit
      1. Communicating with CloudKit for the first time
      2. Listening for changes in the database
      3. Retrieving changes from CloudKit
      4. Configuring your AppDelegate
      5. Storing data in CloudKit
    3. Combining CloudKit and Core Data
      1. Preparing the Core Data models for CloudKit
      2. Importing CloudKit data
      3. Sending Core Data models to CloudKit
    4. Summary
    5. Questions
    6. Further reading
  18. Using Augmented Reality
    1. Understanding ARKit
      1. Understanding how ARKit renders content
      2. Understanding how ARKit tracks the physical environment
    2. Using ARKit Quicklook
      1. Implementing the ARKit Quicklook view controller
    3. Exploring SpriteKit
      1. Creating a SpriteKit scene
    4. Exploring SceneKit
      1. Creating a basic SceneKit scene
    5. Implementing an Augmented Reality gallery
      1. Adding image tracking
        1. Preparing images for tracking
        2. Building the image-tracking experience
      2. Placing your own content in 3D space
    6. Summary
    7. Questions
    8. Further reading
  19. Improving Apps With Location Services
    1. Requesting a user's location
      1. Asking for permission to access location data
      2. Obtaining a user's location
    2. Subscribing to location changes
    3. Setting up geofences
    4. Summary
    5. Questions
    6. Further reading
  20. Making Smarter Apps with CoreML
    1. Understanding machine learning and CoreML
      1. Understanding what machine learning is
      2. Understanding CoreML
        1. Obtaining CoreML models
        2. Using a CoreML model
    2. Combining CoreML and computer vision
      1. Understanding the Vision framework
      2. Implementing an image classifier
    3. Training your own models with CreateML
      1. Training a natural language model
      2. Training a Vision model
    4. Summary
    5. Questions
    6. Further reading
  21. Tracking Activity Using HealthKit
    1. Understanding HealthKit
      1. Requesting access to the HealthKit Store
      2. Storing and retrieving data with HealthKit
    2. Implementing a workout app
    3. Summary
    4. Questions
    5. Further reading
  22. Streamlining Experiences with Siri
    1. Understanding intents
    2. Implementing an intents extension
      1. Understanding app extensions
      2. Configuring your extension
      3. Adding vocabularies to your app
        1. Adding vocabularies through a .plist file
        2. Teaching Siri new vocabularies at runtime
      4. Handling intents in your extension
        1. Resolving the user's input
        2. Confirming the intent status
        3. Performing the desired action
    3. Adding a custom UI to Siri
    4. Implementing Siri Shortcuts
      1. Implementing shortcuts through NSUserActivity
      2. Donating shortcuts with INinteractions
    5. Summary
    6. Questions
    7. Further reading
  23. Using Media in Your App
    1. Playing audio and video
      1. Creating a simple video player
      2. Creating an audio player
        1. Implementing basic audio controls
        2. Implementing the time scrubber
        3. Displaying song metadata
      3. Playing media in the background
    2. Recording video and taking pictures
      1. Taking and storing a picture
      2. Recording and storing video
    3. Manipulating photos with Core Image
    4. Summary
    5. Questions
    6. Further reading
  24. Implementing Rich Notifications
    1. Gaining a deep understanding of notifications
    2. Scheduling and handling notifications
      1. Registering for notifications
      2. Creating notification contents
        1. Creating push notifications
        2. Creating local notifications
      3. Scheduling your notification
        1. Scheduling a timed notification
        2. Scheduling a calendar-based notification
        3. Scheduling a location-based notification
      4. Handling notifications
        1. Handling notifications in your app
        2. Managing pending and delivered notifications
        3. Adding actions to notifications
    3. Implementing grouped notifications
      1. Grouping notifications based on thread identifiers
      2. Providing a custom summary message for your notification group
    4. Implementing notification extensions
      1. Adding a service extension to your app
      2. Adding a content extension to your app
    5. Summary
    6. Questions
    7. Further reading
  25. Instant Information with a Today Extension
    1. Understanding the anatomy of a Today Extension
      1. Finding Today Extensions in iOS
      2. Understanding Today Extensions
    2. Adding a Today Extension to your app
    3. Summary
    4. Questions
    5. Further reading
  26. Exchanging Data With Drag And Drop
    1. Understanding the drag and drop experience
      1. Understanding UIDragInteractionDelegate
      2. Understanding UIDropInteractionDelegate
    2. Implementing a basic drag and drop functionality
      1. Adding drag and drop to a plain UIView
      2. Adding drag and drop to a UICollectionView
    3. Customizing the drag and drop experience
    4. Summary
    5. Questions
    6. Further reading
  27. Improved Discoverability with Spotlight and Universal Links
    1. Understanding Spotlight search
    2. Adding your app contents to the Spotlight index
      1. Indexing your app through user activity
      2. Indexing with CSSearchableItem
        1. Containing information in CSSearchableItemAttributeSet
        2. Adding CSSearchableItem instances to the search index
        3. Safely combining indexing methods
        4. Handling searchable item selection
      3. Understanding Spotlight best practices and ratings
        1. Adding metadata to your web links
        2. Registering as an indexing delegate
    3. Increasing your app's visibility with Universal Links
      1. Preparing your server for Universal Links
      2. Handling Universal Links in your app
    4. Summary
    5. Questions
    6. Further reading
  28. Extending iMessage
    1. Understanding iMessage apps
    2. Creating an iMessage sticker pack
      1. Optimizing assets for your stickers
      2. Creating a custom sticker app
    3. Implementing custom, interactive iMessage apps
      1. Understanding the iMessage app life cycle
      2. Implementing the custom compact view
      3. Implementing the expanded view
    4. Understanding sessions, messages, and conversations
      1. Composing a message
      2. Sending a message
    5. Summary
    6. Questions
    7. Further reading
  29. Ensuring App Quality with Tests
    1. Testing logic with XCTest
      1. Understanding what it means to test code
        1. Determining which tests to write
        2. Choosing the correct test type
          1. Unit tests
          2. Integration tests
        3. Isolating tests
          1. Arrange
          2. Act
          3. Assert
      2. Setting up a test suite with XCTest
      3. Optimizing code for testability
        1. Introducing the question loader
        2. Mocking API responses
        3. Using models for consistency
    2. Gaining insights through code coverage
    3. Testing the user interface with XCUITest
      1. Making your app accessible to your tests
      2. Recording UI tests
      3. Passing launch arguments to your app
      4. Making sure the UI updates as expected
    4. Summary
    5. Questions
    6. Further reading
  30. Discovering Bottlenecks with Instruments
    1. Exploring the Instruments suite
    2. Discovering slow code
    3. Closing memory leaks
      1. Understanding what a memory leak is
        1. Preventing objects from using infinite memory
        2. Avoiding reference cycles
      2. Discovering memory leaks
    4. Creating your own Instrument
      1. Adding signpost logging to your app
      2. Building an Instruments Package
    5. Summary
    6. Questions
    7. Further reading
  31. Offloading Tasks with Operations and GCD
    1. Writing asynchronous code
      1. Understanding threads
      2. Using dispatch queues in your application
    2. Creating reusable tasks with Operations
      1. Using Operations in your apps
    3. Summary
    4. Questions
    5. Further reading
  32. Submitting Your App to the App Store
    1. Adding your application to App Store Connect
    2. Packaging and uploading your app for beta testing
    3. Preparing your app for launch
    4. Summary
    5. Questions
    6. Further reading
  33. Answers
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6 
    7. Chapter 7 
    8. Chapter 8 
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Chapter 18 
    19. Chapter 19
    20. Chapter 20
    21. Chapter 21
    22. Chapter 22
    23. Chapter 23
    24. Chapter 24
    25. Chapter 25 
    26. Chapter 26 
  34. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering iOS 12 Programming - Third Edition
  • Author(s): Donny Wals
  • Release date: October 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789133202