Ive created a login view but as soon as I leave that view all the textfield and password field is empty.

I want the details entered in textfield and password should stay even after changing the view or closing the app and the person should be logged in even after he closes the app, Can anyone help me out?

import SwiftUI

import Firebase



struct login: View {



    @State var chec:String=""

    @State var email:String=""

    @State var navigated = false

    @State var loggedin:Bool=false

    @State var pass:String=""

    @State private var secured:Bool=true



    var body: some View

    {
                 VStack

                {

                    if(loggedin==true)

                    {

                    Image("logo")

                        .resizable()

                        .clipShape(Circle())

                        .shadow(color:.green,radius: 10)

                        .frame(width: 200, height: 200, alignment: .center)

                    }

                    else

                    {

                        Image("logo")

                            .resizable()

                            .clipShape(Circle())

                            .shadow(color:.red,radius: 10)

                            .frame(width: 200, height: 200, alignment: .center)

                    }

                    Spacer(minLength: 50.0)

                    Text(chec)

                        .foregroundColor(.red)

                    HStack

                    {

                        Text("E-mail:")

                        TextField("", text:$email)

                            .foregroundColor(.white)

                            .frame(width: 275)

                            .textFieldStyle(.roundedBorder)

                            .autocapitalization(.none)

                            .disableAutocorrection(true)

                    }

                        HStack

                        {

                        Text("Password:")

                            if secured

                            {

                                SecureField("",text:$pass)

                                    .background(Color.black)

                                    .foregroundColor(.white)

                                    .textFieldStyle(.roundedBorder)

                                    .autocapitalization(.none)

                                    .disableAutocorrection(true)

                            }

                            else

                            {

                                TextField("",text: $pass)

                                    .background(Color.black)

                                    .foregroundColor(.white)

                                    .textFieldStyle(.roundedBorder)

                                    .autocapitalization(.none)

                                    .disableAutocorrection(true)

                            }

                            Button(action: {self.secured.toggle()})

                            {

                                if secured

                                {

                                    Image(systemName:"eye.slash")

                                }

                                else

                                {

                                    Image(systemName:"eye")

                                }

                            }.buttonStyle(BorderlessButtonStyle())

                        }

                    VStack{


                        Button(action: {

                            if(email==""&&pass=="")

                            {

                                chec="Enter E-mail and Password"

                            }

                            else if(pass=="")

                            {

                                chec="Enter Password"

                            }

                            else if(email=="")

                            {

                                chec="Enter E-mail"



                            }

                            guard !email.isEmpty,!pass.isEmpty else

                        {

                            return

                        }

                            loginfunc()

                        }){

                            Text("Login")

                        }

                        NavigationLink(destination: signup(),label:{Text("Not Yet signed? Create new account")})

                            .padding(.top, 3.0)

                    }

                    .padding()

                    Spacer(minLength: 400)

                }

            

        }

    func loginfunc(){

        Auth.auth().signIn(withEmail: email, password: pass) { result, error in

            if error != nil{

                print(error?.localizedDescription ?? "")

                loggedin=false

                chec="E-mail or Password wrong!"

            }else {

                print("success")

                loggedin=true

            }

            if(loggedin==true)

            {

                chec=""

                print("wow so nice to get work done")

            }

        }

    }

}

//}

struct ButtonView: View {

    var buttext:String

    var body: some View {

        Text(buttext)

        .frame(width: 200, height: 100, alignment: .center)

        .background(Color.black)

        .foregroundColor(Color.white)

    }

}

struct login_Previews: PreviewProvider {

    static var previews: some View {

        login()

            .environment(\.colorScheme, .dark)

    }

}


Use UserDefaults or @AppStorage

Ive created a login view but as soon as I leave that view all the textfield and password field is empty.
 
 
Q