Skip to main content

What is the @SceneStorage property wrapper?

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

What is the @SceneStorage property wrapper? 관련

SwiftUI by Example

Back to Home

What is the @SceneStorage property wrapper? | SwiftUI by Example

What is the @SceneStorage property wrapper?

Updated for Xcode 15

If you want to save unique data for each of your screens, you should use SwiftUI’s @SceneStorage property wrapper. This works a bit like @AppStorage in that you provide it with a name to save things plus a default value, but rather than working with UserDefaults it instead gets used for state restoration – and it even works great with the kinds of complex multi-scene set ups we see so often in iPadOS.

For example, if you have a text editor and want to store what the user was typing, you should use this kind of code:

struct ContentView: View {
    @SceneStorage("text") var text = ""

    var body: some View {
        NavigationStack {
            TextEditor(text: $text)
        }
    }
}

Because that uses @SceneStorage, SwiftUI will automatically make sure that each scene instance has its own copy of the text – if you run the app side by side both will save and restore their data correctly.

Now, before you use @SceneStorage there are some important warnings from Apple:

  • Don’t save lots of data; save just what you need for state restoration.
  • Never store sensitive data in scene storage, because it isn’t secure.
  • If the user goes to the app switcher and destroys your app, the scene storage is also destroyed.
Similar solutions…
All SwiftUI property wrappers explained and compared | SwiftUI by Example

All SwiftUI property wrappers explained and compared
What is the @GestureState property wrapper? | SwiftUI by Example

What is the @GestureState property wrapper?
What is the @Published property wrapper? | SwiftUI by Example

What is the @Published property wrapper?
What is the @ScaledMetric property wrapper? | SwiftUI by Example

What is the @ScaledMetric property wrapper?
What is the @ObservedObject property wrapper? | SwiftUI by Example

What is the @ObservedObject property wrapper?

이찬희 (MarkiiimarK)
Never Stop Learning.