cd-himthatguy
cd-himthatguy

Reputation: 11

how toshow alert in SwiftUI that Email and Password are incorrect?

Good Evening,

I have a question, I need to show an error when a user enters a wrong email or enters a wrong password. What do I need to add in my code? I am new to software development and Sam not so familiar with swift ui could someone help me?

    @State var email = ""
    @State var password = ""



    var body: some View {
        Image("ISD-Logo")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 185, height: 140)
            .clipped()
            .padding(.bottom, 75)
        
        VStack {
            TextField("Email", text: $email)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            SecureField("password", text: $password)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            Button(action: { login() }) {
                Text("Sign in")
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .frame(width: 220, height: 60)
                    .background(Color.black)
                    .cornerRadius(35.0)
            }
        }
        .padding()
    }
    

    //    Login and error message
    func login() {
        Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
            if error != nil {
                print(error?.localizedDescription ?? "")
            } else {
                print("success")
                }
            }
        }
    }

}
struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}

Upvotes: 0

Views: 1107

Answers (2)

Sishu
Sishu

Reputation: 1558

You can try like this. I would suggest you go to SWIFT UI basics first

@State var email = ""
    @State var password = ""



    var body: some View {
        Image("ISD-Logo")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 185, height: 140)
            .clipped()
            .padding(.bottom, 75)
        
        VStack {
            TextField("Email", text: $email)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            SecureField("password", text: $password)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            Button(action: { login() }) {
                Text("Sign in")
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .frame(width: 220, height: 60)
                    .background(Color.black)
                    .cornerRadius(35.0)

            }
        }
        .padding()
    }
    

    //    Login and error message
    func login() {
        Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
            if error != nil {
             self.isAlert = true
                print(error?.localizedDescription ?? "")
            } else {
                print("success")
                }
            }
        }
    }

}
struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}

Upvotes: 0

BLANDIN Florian
BLANDIN Florian

Reputation: 56

What kind of alert do you want?

You can do that:

struct ContentView: View {
    @State private var showingAlert = false

    var body: some View {
        Button("Show Alert") {
            showingAlert = true
        }
        .alert("Important message", isPresented: $showingAlert) {
            Button("OK", role: .cancel) { }
        }
    }
}

Upvotes: 1

Related Questions