Reader small image

You're reading from  SwiftUI Cookbook - Third Edition

Product typeBook
Published inDec 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805121732
Edition3rd Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Juan C. Catalan
Juan C. Catalan
author image
Juan C. Catalan

Juan C. Catalan is a software engineer with more than 18 years of professional experience. He started mobile development back in the days of iOS 3. Juan has worked as a professional iOS developer in many industries, including medical devices, financial services, real estate, document management, fleet tracking and industrial automation. He has contributed to more than 30 published apps in the App Store, some of them with millions of users. Juan gives back to the iOS development community with technical talks, mentoring developers, reviewing technical books and now as a book author. He lives in Austin, Texas, with his wife Donna, where they spend time with their kids.
Read more about Juan C. Catalan

Right arrow

Preface

SwiftUI is the modern way to build user interfaces for all Apple platforms. SwiftUI code is easy to read and write because it uses Swift’s declarative programming syntax. The new edition of this comprehensive cookbook includes fully updated content, code samples and a companion GitHub repository for SwiftUI 5, iOS 17, Xcode 15, and Swift 5.9.

This book covers the foundations of SwiftUI, as well as the new features of SwiftUI 5 introduced in iOS 17. Particular attention is given to illustrating the interaction between SwiftUI and the rest of an app’s code, such as concurrency with Combine and async/await, persistence with Core Data and Swift Data, and data visualization with Swift Charts.

With over 100 recipes for real-world user interfaces packed with best-coding practices, you’ll learn how to use SwiftUI to build visually compelling apps, from simple lists to complex interactive charts.

Who this book is for

This book is for mobile developers who want to learn SwiftUI as well as for experienced iOS developers transitioning from UIKit to SwiftUI. The book assumes knowledge of the Swift programming language. You’ll also find this book to be a helpful resource if you’re looking for reference material regarding the implementation of various features in SwiftUI.

What this book covers

Chapter 1, Using the Basic SwiftUI Views and Controls, explains the basic building blocks for creating SwiftUI apps and how to integrate UIKit components in SwiftUI views.

Chapter 2, Displaying Scrollable Content with Lists and Scroll Views, explains how to use using List views and ScrollView containers to display content when it does not fit on a single screen. You’ll also be able to learn about editable and searchable lists.

Chapter 3, Exploring Advanced Components, explains how to use LazyStack and Lazy Grid to improve performance when displaying large datasets. Also, you’ll learn how to programmatically scroll with ScrollViewReader, display hierarchical data in an expanding list, and hide and show content with DisclosureGroup. Finally, you’ll learn how to display glanceable content outside your app using SwiftUI Widgets.

Chapter 4, Viewing while Building with SwiftUI Preview in Xcode 15, explains how to use the preview macro introduced with Swift 5.9 with the powerful live preview in the Xcode 15 canvas.

Chapter 5, Creating New Components and Grouping Views with Container Views, explains how to use Form views for user interaction, Table for multi-column lists, and Grid for powerful two-dimensional layouts.

Chapter 6, Presenting Views Modally, explains how to display custom views for narrow and focused user interaction, using alerts, confirmation dialogs, sheets, popovers, and context menus.

Chapter 7, Navigation Containers, explains how to use NavigationStack for hierarchical, data-driven navigation, NavigationSplitView for multi-column user interfaces, and TabView for navigation among non-hierarchical content.

Chapter 8, Drawing with SwiftUI, explains how to implement drawings in SwiftUI by using built-in shapes and drawing custom paths and polygons.

Chapter 9, Animating with SwiftUI, explains how to implement basic animations, spring animations, implicit and delayed animations, as well as how to combine transitions, create custom transitions, and create asymmetric transitions. Additionally, you’ll also be able to learn how to perform chained animations with PhaseAnimator and full-custom animations with KeyFrameAnimator.

Chapter 10, Driving SwiftUI with Data, explains how to use the SwiftUI binding mechanism to populate and change views when the bounded data changes. At the end of the chapter, you’ll learn about the Observation framework and the Observable macro, introduced with iOS 17.

Chapter 11, Driving SwiftUI with Combine, explains how to integrate Combine to drive the changes of the SwiftUI views. You’ll explore how to validate forms, fetch data asynchronously from the network, and test Combine-based apps.

Chapter 12, SwiftUI Concurrency with async await, explains how to implement Swift concurrency with async await, fetching from a network resource, and creating an infinite scrolling page.

Chapter 13, Handling Authentication and Firebase with SwiftUI, explains how to implement authentication in your app and store app data in a cloud database.

Chapter 14, Persistence in SwiftUI with Core Data and Swift Data, explains how to implement persistence using SwiftUI and Core Data. You’ll learn how to save, delete, and modify objects in a Core Data local database. You’ll also learn how to use Swift Data, the new persistence framework introduced with iOS 17.

Chapter 15, Data Visualization with Swift Charts, explains how to use Swift Charts for data visualization. You’ll learn how to plot different types of charts using a few building blocks and powerful view modifiers. You’ll discover interactive charts and range charts using real-world examples. You’ll also learn how to transform your data to simplify the visualization process.

Chapter 16, Creating Multiplatform Apps with SwiftUI, explains how to create a multiplatform SwiftUI app that works on iOS 17, macOS 14 Sonoma, and watchOS 10.

Chapter 17, SwiftUI Tips and Tricks, covers several tips and tricks of SwiftUI that will help you to solve several common problems, such as testing SwiftUI views, using custom fonts, showing PDF documents, and using Markdown text.

To get the most out of this book

Inform the reader the things that they need to know before they start: spell out what knowledge you are assuming. Tell the reader anything they need to know before they start.

This book has been completely revised for Swift 5, iOS 17, Xcode 15, and Swift 5.9.

To build all the apps in this book, you will need:

  • A Mac computer running macOS Ventura 13.5 or later. For Chapter 16, you’ll need macOS 14.0 Sonoma or later.
  • Xcode 15.0 or later.

The book extensively uses the live preview of the Xcode canvas to introduce the learning material and occasionally uses the iOS simulator. If you want to test the apps on your iPhone or iPad, you will need to obtain a free Apple Developer account from Apple at https://developer.apple.com.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you to avoid any potential errors related to the copying and pasting of code.

Download the example code files

The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/SwiftUI-Cookbook-3rd-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781805121732.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system.”

A block of code is set as follows:

Chart {
    ForEach(data) { entry in
        BarMark(
            x: .value("Question", entry.question),
            y: .value("Count", entry.count)
        )
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

struct TodoItem: Identifiable {
    let id = UUID()
    var title: String
}

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “Tap on the + button a few times and then on the Refresh button”.

Warnings or important notes appear like this.

Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There’s more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the step-by-step instructions required to build the app of the recipe. Screenshots and explanatory figures are provided along with the code snippets.

How it works…

This section usually consists of a detailed explanation of the steps followed in the previous section. It explains in detail the code snippets and technologies used in the recipe.

There’s more…

This optional section covers additional topics related to the recipe to make you more knowledgeable about the technologies used to build the app.

See also

This optional section provides helpful links to other useful information about the technologies used in the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email feedback@packtpub.com and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Share your thoughts

Once you’ve read SwiftUI Cookbook, Third Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application. 

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781805121732

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
SwiftUI Cookbook - Third Edition
Published in: Dec 2023Publisher: PacktISBN-13: 9781805121732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Juan C. Catalan

Juan C. Catalan is a software engineer with more than 18 years of professional experience. He started mobile development back in the days of iOS 3. Juan has worked as a professional iOS developer in many industries, including medical devices, financial services, real estate, document management, fleet tracking and industrial automation. He has contributed to more than 30 published apps in the App Store, some of them with millions of users. Juan gives back to the iOS development community with technical talks, mentoring developers, reviewing technical books and now as a book author. He lives in Austin, Texas, with his wife Donna, where they spend time with their kids.
Read more about Juan C. Catalan