skip to Main Content

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()
    }
}

2

Answers


  1. 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) { }
            }
        }
    }
    Login or Signup to reply.
  2. 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()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search