Fatal error: @SceneStorage is only for use with SwiftUI App Lifecycle.: file SwiftUI, line 0

Viewed 404

I'm trying to use the new property wrapper for iOS 14 "SceneStorage" though it is producing this error:

Fatal error: @SceneStorage is only for use with SwiftUI App Lifecycle.: file SwiftUI, line 0

Here is my code:

struct ContentView: View {
    @SceneStorage("isLoggedIn") var isLoggedIn = true
    
    var body: some View {
        Text("Hello, World!).onAppear {
            print($isLoggedIn)
        }
    }
}

Updated.... Thanks, @Asperi!. Make sure you set the Life Cycle to SwiftUI App.

enter image description here

1 Answers

Works fine with Xcode 12 / iOS 14 / SwiftUI Life-cycle

It looks like your project uses SwiftUI 1.0 AppDelegate/SceneDelegate (aka UIKit Life-cycle)

In SwiftUI Life-cycle your app main should be like

@main
struct YourAppName: App {
    var body: some Scene {
        WindowGroup {       // << this introduces Scene, needed for SceneStorage
            ContentView()
        }
    }
}
Related