Member-only story
Mastering AppStorage
in SwiftUI & iOS
AppStorage
in SwiftUI & iOSSwiftUI introduced the @AppStorage
property wrapper with iOS 14, making it incredibly easy to store and retrieve small data values using UserDefaults. Whether you're building a welcome screen or saving user preferences, @AppStorage
can help you cut down on boilerplate code and keep your UI reactive.
Read this for free
In this article, we will explore what @AppStorage
is, how it compares to UserDefaults
, and go through a few examples.
What is UserDefaults
?
UserDefaults
is Apple’s lightweight storage system for saving small bits of data. It’s perfect for values like:
- User onboarding state
- User-selected themes (light/dark mode)
- Default username or brand name
- Any small bit of app setting that needs to persist
Here’s what saving and retrieving a string using UserDefaults
looks like:
UserDefaults.standard.set("DevTechie.com", forKey: "brand")
let storedBrand =…