Mastering AppStorage in SwiftUI & iOS

  • Apr 7, 2025

Mastering AppStorage in SwiftUI & iOS

SwiftUI 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.

SwiftUI 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.

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 = UserDefaults.standard.string(forKey: "brand")

It works, but it’s not very SwiftUI-friendly — there’s no built-in binding and it doesn’t trigger view updates. 

The@AppStorage solves these problems by integrating directly into SwiftUI views. It automatically reads and writes to UserDefaults using a key, and it keeps your view updated when the value changes.

Here is the syntax to follow to store a value in AppStorage.

@AppStorage("keyName") var variableName: DataType = defaultValue

Here is how we use this to store DevTechie brand name

@AppStorage("brand") var brand: String = "DevTechie"

Let’s build a simple UI to enter and persist brand name into the AppStorage.

import SwiftUI

struct AppStorageExample: View {
    @AppStorage("brand") var brand: String = "DevTechie"

    var body: some View {
        VStack(spacing: 20) {
            Text("Brand Name: \(brand)")
                .font(.headline)

            TextField("Enter brand name", text: $brand)
                .textFieldStyle(.roundedBorder)
                .padding()

            Button("Reset to Default") {
                brand = "DevTechie"
            }
        }
        .padding()
    }
}

The @AppStorage supports the following types out of the box:

  • String

  • Int

  • Double

  • Bool

  • URL

  • Data

For more complex types, we can use Codable and save it as Data.

Let’s look at another example and save boolean value in the AppStorage. We will bind the AppStorage property to the Toggle view.

struct AppStorageExample: View {
    @AppStorage("isDarkMode") var isDarkMode: Bool = false

    var body: some View {
        VStack(spacing: 20) {
           Text("DevTechie.com")
                .font(.largeTitle)
            
            Toggle("Dark Mode", isOn: $isDarkMode)
        }
        .preferredColorScheme(isDarkMode ? .dark : .light)
        .padding()
    }
}

The @AppStorage wrapper is a game-changer for building persistent, state-driven UIs in SwiftUI. It allows you to eliminate boilerplate UserDefaults code, reactively update views and Save & retrieve values seamlessly.