0
votes

I've been trying to present a UIAlertController when user entered wrong password for their account, the UIAlertController is located in one separate file inside a Model group which I extend the UIViewController class to add this alert functionality to it. I also has another file inside my model group namely LogIn which I wrote all the logic behind the login process so that I can call it to my LogInVC. However, I got an error of "Attempt to present on whose view is not in the window hierarchy!" whenever the function get call inside my LogInVC. I'm trying to make my project in MVC and I know what caused this error but I just don't know how to fix it. May anyone tell me how to fix this problem?

Alert

import Foundation
import UIKit

extension UIViewController {

    //MARK: - Not Enough Information Alert
    func notEnoughInfo(title: String, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(OKAction)
        self.present(alertController, animated: true, completion: nil)
    }

    //MARK: - Incorrect Username and Password
    func wrongInfo(title: String, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "Try again", style: .default, handler: nil)
        alertController.addAction(OKAction)
        self.present(alertController, animated: true, completion: nil)
    }


LogIn

import Foundation
import Firebase

class LogIn: UIViewController{

    let db = Firestore.firestore()

    //MARK: - userValidation()
    func userValidation(Username:String, Password:String){

        db.collection("users").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                if let snapShotDocument = querySnapshot?.documents {
                    for doc in snapShotDocument {
                        let data = doc.data()
                            if let username = data[C.username] as? String, let password = data[C.password] as? String {
                                if Username == username, Password == password {
                                    print("Log in Successfully")
                                }
                                else {
                                    self.wrongInfo(title: "Incorrect password", message: "Try again please")
                                }
                            }
                    }
                }
            }
        }
    }
}

LogInVC

import UIKit
import Firebase

class LogInVC: UIViewController {


    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var logInBtn: UIButton!
    let db = Firestore.firestore()
    let logIn = LogIn()

    override func viewDidLoad() {
        super.viewDidLoad()
        //logInBtn.layer.cornerRadius = logInBtn.frame.height/5
    }

    @IBAction func logInBtn(_ sender: UIButton) {
        if let username = emailTextField.text, let password = passwordTextField.text{
            if username.isEmpty || password.isEmpty{
                notEnoughInfo(title: "Not enough information", message: "Please fill in all the necessary information.")
            }else{
                logIn.userValidation(Username: username, Password: password) //here is where problem occured
                //move to another viewcontroller
            }
        }
    }

    @IBAction func signUpBtn(_ sender: UIButton) {
        let push = storyboard?.instantiateViewController(withIdentifier: C.signUpVC) as! SignUpVC
        push.modalPresentationStyle = .fullScreen
        present(push, animated: true, completion: nil)
    }

} //ends of class
1

1 Answers

0
votes

You need to first dismiss the current present alert or present controller. currently you are trying to present controller over a controller that's why it shows this error. Don't present . remove this line from self.wrongInfo(title: "Incorrect password", message: "Try again please") from LogIn.

try this and you can comment again if there is anything regarding this.