Latest blog post
Understanding SwiftUI Preferences - SwiftUI Parent / Child View Communication
I’m currently working on three different apps that have one thing in common - they all need authentication!
So in an attempt to keep things DRY, I decided to build out the authentication functionality (I.e., login / sign-up screens, an account screen, etc.) in one of the apps, and copy and paste the code into the other apps.
Needless to say this quickly became a maintenance nightmare…
Last week, I decided I’ve had enough, so I extracted the code into a separate Swift package that is properly reusable (I’ll make it available on my GitHub profile once I’ve cleaned up a bit).
A main design goal of this package is to create opinionated, yet configurable screens. To achieve this, I’ve encapsulated the UI elements for the individual authentication mechanisms in configurable SwiftUI views.
I quickly found myself in need of a way to expose the inner state of these components to the main view, for example to display error message that occurred inside the email/password view. This is a classic use case for SwiftUI’s Preferences.
Preferences are one of the underrated aspects of SwiftUI - their name doesn’t really give away their inherent power.
In this blog post, I describe what they are, where they’re used in SwiftUI, and how you can use them in your own apps.
Oh, and in case you’ve tried to use SwiftUI preferences in Swift 6 language mode and ran into some issues - don’t fret, I’ll show you how to solve this.
Coming up
Building a Second Brain app, S2 E4: Genkit / Codable
In the most recent episode of “Building a Second Brain app”, I demonstrated how to call Genkit flows from a Swift app.
I relied on Xcode’s predictive code completion a bit too much, which resulted in calling the flows using plain strings as parameters, which resulted in all sorts of follow-on errors. So in the next episode, I’m going to refactor this a bit, and use Swift’s Codable protocol to map the data we’re passing to and from the Genkit flow.
And - since Genkit is going GA, we will also take a look at the changes this requires (not too many, as you will be pleased to hear).
Tune in on Tuesday, February, 4th, at the usual time!
From the community
A Swift Concurrency Glossary
Many developers use the term "async/await" when referring to Swift's concurrency system, which isn't quite accurate, as Swift concurrency is a lot more than "just" asynchronous calling of functions.
Matt Massicotte put together an entire glossary, and it's honestly quite impressive.
Observing properties on an @Observable class outside of SwiftUI views
There has been a flurry of blog posts about the Swift Observation framework. Donny's conclusion pretty much reflects my own experiences with using Observation's withObservationTracking
- it's a bit cumbersome to use, and it definitely feels like it's been predominantly implemented with SwiftUI in mind.
If you want to subscribe to changes on a specific property, it might be worthwhile considering using Combine - this might be the more straightforward solution.
SwiftUI Tutorials
A while ago, Apple released an updated version of their Introducing SwiftUI tutorial. In addition to a hands-on introduction to SwiftUI, this now also covers the Observation framework and modern navigation.
Even if you are a seasoned SwiftUI developer, it's worth checking this out - it's always good to see how Apple thinks about their own APIs and how they should be used in apps.
SwiftUI Button Image When Pressed
In this post, Keith explains how to create a button style that shows different button images while the user is pressing the button - including an animation!
I ran into a similar situation fairly recently- I needed to override the button behaviour in order to run a piece of code when the user presses the button, which can be achieved by creating a custom button style based on PrimitiveButtonStyle
. I wrote about this here.
Exploring Interactive Bottom Sheets in SwiftUI
This came out literally after I just had hit "publish" on my ActionMenu package, and I was curious to learn if they'd cover some aspects of bottom sheets I hadn't considered in my implementation.
And - lo and behold - there are a couple of features I didn't know about. For example, did you ever wonder how apps like Apple Maps or Google Maps are able to display a bottom sheep and keep the main content interactive? Well, you can control this behaviour using the presentationBackgroundInteraction
view modifier.
Jobs (Sponsored)
Senior iOS Engineer @ visiolab
Build the future of self-checkout with cutting-edge AI, all in a SwiftUI-based iPad app powered by Firebase 🚀
Ready to dive into Edge AI and craft the best checkout experience for customers across Europe and the US? Apply now!
Comment
Looking back at the past couple of weeks, I’ve noticed that I’ve spent quite a bit of time trying to figure out how SwiftUI works under the hood.
Others have too. I found episode 409 of SwiftTalk quite enlightening - Chris and Florian no doubt are some of the most knowledgeable people about SwiftUI outside of Apple, but it seems that doesn't preclude you from being puzzled by how SwiftUI works.
Figuring out how SwiftUI works under the hood would be a lot easier if it was open source, obviously. It’d be easier to understand if a certain behaviour is a bug or intentional, it’d be easier to understand exactly how the SwiftUI environment and preferences work, and - not least - it would be inspiring to see how some of the more complex views are implemented.
So here is my pleasure to Apple: please open source SwiftUI - everyone will win!
PS - you might notice that there is a sponsored link in this newsletter now. That's right - you can sponsor this newsletter now! I will make sure that all sponsored links (and job listings) are relevant to the topics I cover, so the links should never feel like spam. If you ever feel differently, please let me know by replying to this email.
Peter