0
0
iOS Swiftmobile~20 mins

UserDefaults for simple values in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Settings Screen
A screen where the user can toggle a switch and enter their name. The app saves these values using UserDefaults so they stay saved when the app restarts.
Target UI
-------------------------
| Settings              |
|-----------------------|
| Name: [___________]   |
|                       |
| Enable Notifications:  |
| [  Switch  ]          |
|                       |
| [ Save ]              |
-------------------------
A TextField to enter the user's name
A Switch to toggle notifications on or off
A Save button that saves the name and switch state to UserDefaults
When the screen loads, it should read saved values from UserDefaults and update the UI
Use UserDefaults.standard for saving and loading
Starter Code
iOS Swift
import SwiftUI

struct SettingsView: View {
    @State private var name: String = ""
    @State private var notificationsEnabled: Bool = false

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("User Info")) {
                    TextField("Enter your name", text: $name)
                }
                Section(header: Text("Preferences")) {
                    Toggle(isOn: $notificationsEnabled) {
                        Text("Enable Notifications")
                    }
                }
                Button("Save") {
                    // TODO: Save name and notificationsEnabled to UserDefaults
                }
            }
            .navigationTitle("Settings")
            // TODO: Load saved values from UserDefaults when view appears
        }
    }
}

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView()
    }
}
Task 1
Task 2
Solution
iOS Swift
import SwiftUI

struct SettingsView: View {
    @State private var name: String = ""
    @State private var notificationsEnabled: Bool = false

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("User Info")) {
                    TextField("Enter your name", text: $name)
                }
                Section(header: Text("Preferences")) {
                    Toggle(isOn: $notificationsEnabled) {
                        Text("Enable Notifications")
                    }
                }
                Button("Save") {
                    UserDefaults.standard.set(name, forKey: "userName")
                    UserDefaults.standard.set(notificationsEnabled, forKey: "notificationsEnabled")
                }
            }
            .navigationTitle("Settings")
            .onAppear {
                if let savedName = UserDefaults.standard.string(forKey: "userName") {
                    name = savedName
                }
                notificationsEnabled = UserDefaults.standard.bool(forKey: "notificationsEnabled")
            }
        }
    }
}

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView()
    }
}

This solution uses UserDefaults.standard to save and load simple values.

When the Save button is tapped, it saves the current name string and notificationsEnabled boolean to UserDefaults with keys "userName" and "notificationsEnabled".

When the view appears, it reads these values back from UserDefaults. If a saved name exists, it updates the name state. It also reads the boolean for notifications, defaulting to false if not set.

This way, the user's settings persist between app launches, and the UI always shows the saved values.

Final Result
Completed Screen
-------------------------
| Settings              |
|-----------------------|
| Name: [John Doe   ]   |
|                       |
| Enable Notifications:  |
| [  ON Switch  ]       |
|                       |
| [ Save ]              |
-------------------------
User types their name in the text field
User toggles the switch on or off
User taps Save to store the values
When the screen opens again, the saved name and switch state appear automatically
Stretch Goal
Add a Reset button that clears the saved name and notifications setting from UserDefaults and resets the UI to default values.
💡 Hint
Use UserDefaults.standard.removeObject(forKey:) for each saved key and reset the @State variables to default values.