Learn SwiftUI

Book description

Get to grips with Apple's new SwiftUI framework for creating robust UIs for iOS and iPadOS using Swift programming

Key Features

  • Use SwiftUI for building dynamic apps for Apple devices from scratch
  • Understand declarative syntax in cross-platform development and how states work within SwiftUI
  • Learn to develop watchOS apps by reusing SwiftUI code

Book Description

SwiftUI is the new and powerful interface toolkit that lets you design and build iOS, iPadOS, and macOS apps using declarative syntax. It is a powerful way to develop the UI elements of applications, which would normally be tightly coupled to application logic.

Learn SwiftUI will get you up to speed with the framework and cross-device UI development in no time. Complete with detailed explanations and practical examples, this easy-to-follow guide will teach you the fundamentals of the SwiftUI toolkit. You'll learn how to build a powerful iOS and iPadOS application that can be reused for deployment on watchOS. As you progress, you'll delve into UI and unit testing in iOS apps, along with learning how to test your SwiftUI code for multiple devices. The book will also show you how to integrate SwiftUI features such as data binding and network requests into your current application logic.

By the end of this book, you will have learned how to build a cross-device application using the SwiftUI framework and Swift programming.

What you will learn

  • Explore the fundamentals of SwiftUI and compare it with existing UI frameworks
  • Write SwiftUI syntax and understand what should and shouldn't be included in SwiftUI's layer
  • Add text and images to a SwiftUI view and decorate them using SwiftUI's modifiers
  • Create basic forms, and use camera and photo library functions to add images to them
  • Understand the core concepts of Maps in iOS apps and add a MapView in SwiftUI
  • Design extensions within your existing apps to run them on watchOS
  • Handle networking calls in SwiftUI to retrieve data from external sources

Who this book is for

This SwiftUI book helps any mobile app developer looking to understand the fundamentals of the new SwiftUI framework along with the benefits of cross-device development. A solid understanding of iOS and macOS app development, along with some knowledge of the Swift programming language, will be beneficial. Basic programming knowledge is essential to grasp the concepts covered in the book effectively.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learn SwiftUI
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    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. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with SwiftUI
    1. Technical requirements
    2. Introducing Swift as a programming language
    3. Learning about existing UI frameworks
      1. Creating the UI programmatically
      2. Creating a UI via Interface Builder
    4. Introducing SwiftUI
      1. What is SwiftUI?
      2. Syntax in SwiftUI
      3. States and updating the UI
      4. Tools and features
      5. Building for multiple devices
    5. When to use SwiftUI, and why
    6. Summary
    7. Questions
    8. Further reading
  8. Understanding Declarative Syntax
    1. Technical requirements
    2. What is declarative syntax?
    3. Visualizing declarative syntax
      1. Getting started with SwiftUI in Xcode
      2. Making a "Hello World" app
      3. Returning multiple views
    4. Nesting and decoration
      1. Modifiers
      2. Nesting syntax
      3. Grouping
    5. Imperative syntax
    6. Summary
    7. Questions
    8. Further reading
  9. Building Layout and Structure
    1. Technical requirements
    2. Understanding UI logic – the MVVM architecture
      1. MVVM overview
      2. MVVM in SwiftUI
      3. Other architecture patterns
    3. Design patterns in SwiftUI
      1. Observable objects
      2. Publishing objects
    4. Summary
    5. Questions
    6. Further reading
  10. Creating Your First Application
    1. Technical requirements
    2. Xcode, as an IDE
      1. Creating our first project
    3. Core components of Xcode
      1. Navigator
      2. Scheme and device list
      3. Automatic Preview
      4. Xcode Simulator
    4. Mock data in Automatic Preview
      1. Understanding Automatic Preview
      2. Understanding the Preview Provider
    5. Summary
    6. Questions
    7. Further reading
  11. Understanding Controls, Views, and Lists
    1. Technical requirements
    2. Exploring and understanding Text and decoration
      1. Text options
      2. Updating PreviewProvider
      3. A little more Text decoration
    3. Creating custom Views in Lists
      1. Creating a custom view
      2. Working independently with our new custom view
    4. Adding more controls
      1. Buttons
      2. Images
      3. Segmented (picker) contols
    5. Summary
    6. Further reading
  12. Working with Navigation in SwiftUI
    1. Technical requirements
    2. Creating additional Views
      1. Creating the recipe details View
      2. Updating our mock data
      3. Testing the new view in the simulator
    3. App navigation
      1. Adding navigation to our ContentView
    4. Accessing with @EnvironmentObject
      1. Adding and injecting the @EnvironmentObject class
      2. Using @EnvironmentObject
      3. Using EnvironmentObject as a single source of truth
      4. EnvironmentObject best practices
      5. Mock EnvironmentObject
    5. Summary
    6. Questions
    7. Further reading
  13. Creating a Form with States and Data Binding
    1. Technical requirements
    2. Creating our recipe form View
      1. Implementing text and text fields
      2. Creating our first custom modifier
      3. Adding a button to the navigation bar
      4. Adding in a bit of a hack
    3. Adding images from our library
      1. Creating our Image View
      2. Implementing our ImageHelper
    4. Adding a multiline text input and country picker
      1. Implementing a UITextView wrapper
      2. Adding a country picker
    5. Persisting our recipe
      1. Persisting our image
      2. Persisting our data
      3. Making the save
    6. Summary
    7. Questions
  14. Networking and Linking to Your Existing App Logic
    1. Technical requirements
    2. Basic networking in SwiftUI
      1. Creating our network helper
      2. Invoking our network request
    3. Integrating UIViews with UIViewRepresentable
      1. Implementing UIViewRepresentable
      2. Customizing our UIViewRepresentable and coordinators
    4. Integrating ViewControllers with UIViewControllerRepresentable
      1. Implementing UIViewControllerRepresentable
    5. Other representable protocols
      1. Options for macOS
      2. Options for watchOS
    6. Summary
    7. Questions
    8. Further reading
  15. Maps and Location Services
    1. Technical requirements
    2. Adding a Map with MapKit control
      1. Implementing MapKit
      2. Adding a MapKit View to SwiftUI
    3. Creating our first pin
      1. Adding your first annotation (pin)
      2. Creating custom annotations
    4. Identifying our location
      1. Creating our MapLocationManager
      2. Hooking up our MapLocationManager to SwiftUI
      3. Location permissions
    5. Piecing it all together
      1. Updating our Helper classes
      2. Hooking up our MapView
      3. Adding an annotation callout accessory and filtering
      4. Adding a reset filter button
    6. Summary
    7. Questions
    8. Further reading
  16. Updating for iPad with NavigationViewStyle
    1. Technical requirements
    2. Updating our project for iPad
      1. Project settings
      2. Portrait and landscape support
      3. Understanding assets
      4. iPadOS simulators
    3. Running our app on iPad for the first time
      1. Running on the simulator
      2. Initial support for list views
    4. Making better use of NavigationViewStyle
      1. Other NavigationViewStyle options
      2. Making use of DoubleColumnNavigationViewStyle
      3. Improving our architecture with DoubleColumnNavigationViewStyle
      4. Cleaner navigation architecture
      5. Final check on our iPhone and iPad app
    5. Summary
    6. Questions
    7. Further reading
  17. SwiftUI on watchOS
    1. Technical requirements
    2. Developing for watchOS
      1. What is WatchKit?
      2. Understanding extensions
      3. Extensions in watchOS
    3. Creating a watchOS project
      1. Updating our project
      2. Changes to our project
    4. Using SwiftUI to create a list of recipes
      1. Creating a List() view in watchOS
      2. Using our mock data
      3. Multiple screens in watchOS
      4. Adding multiple screens
    5. Passing data between our app and watchOS
      1. Initializing WatchConnectivity in iOS
      2. Sending data to watchOS
      3. Receiving data on watchOS
      4. Testing sendMessage() end to end
    6. Summary
    7. Questions
    8. Further reading
  18. SwiftUI versus UIKit
    1. Technical requirements
    2. List view versus UITableView
      1. Basic UITableView implementation
      2. Basic SwiftUI List implementation
    3. Data Binding versus Data Source
      1. UIKit – multiple Data Sources
      2. SwiftUI – handling Data Sources
    4. Comparing decoration techniques
      1. Simple decoration made easy
      2. Complex decoration made easy
    5. Migrating UIKit ready for SwiftUI
      1. UIStackView
      2. UITextField
      3. UISwitch
    6. Summary
    7. Questions
  19. Basic Animation in Views
    1. Technical requirements
    2. The fundamental use of animations
      1. Implicit animations
      2. Explicit animations
    3. Exploring animation options
      1. Easings
        1. .easeIn
        2. .easeOut
        3. .easeInOut
      2. Springs
        1. .spring
        2. .linear
    4. Rotation and scaling
      1. Rotation
      2. 3D rotation
      3. Scaling
    5. Adding animation to our app
      1. Spinning star
      2. Fading image
      3. Rotating the 3D action button
    6. Summary
    7. Questions
    8. Further reading
  20. Animations in Transitions
    1. Technical requirements
    2. Basic transitions
      1. Invoking a basic transition
      2. Transition modifier options
      3. Moving views with transitions
    3. Advanced transitions
      1. Asymmetric transitions
      2. Combined transitions
      3. Using asymmetric, combined, and inline animations together
    4. Adding transitions to our app
      1. Transition validation
      2. Asymmetric transition – loading state
    5. Summary
    6. Questions
  21. Testing in SwiftUI
    1. Technical requirements
    2. Creating a UI/Unit Test project
      1. Creating a UI test project
      2. Writing our very first UI Test
      3. Writing multiple UI Tests
      4. Nesting UI Tests
      5. Testing with assertions
    3. Creating our first Unit Test project and tests
      1. Creating a Unit Test project
      2. Writing our very first Unit Test
      3. Unit Testing conclusion
    4. Debugging in SwiftUI
      1. Understanding breakpoints
      2. Inspecting variables
      3. Understanding the console window
      4. Other tools to consider
        1. Instruments
        2. Analyze
    5. Summary
    6. Questions
    7. Further reading
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learn SwiftUI
  • Author(s): Chris Barker
  • Release date: April 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781839215421