ishkur88 ishkur88 - 2 months ago 21
Swift Question

Showing Alerts from Model, Swift

I'm trying to show an error alert to my users from the Firebase backend.

I separated my Signup function according to MVC and put it in the Model, however the print(error!.localizedDescription) is nested in an if statement inside my model. I can't simply make an alertController because it does not belong to the view in main.storyboard.

How do I pull that error from my Model and display it back on the view through the VC?

MODEL

func signUp(email: String, username: String, password: String, data: NSData!)
{

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil
{

self.setUserInfo(user: user, username: username, password: password, data: data)

}
else
{
print(error!.localizedDescription)
}
})
}


VIEW CONTROLLER

@IBAction func signupButton(_ sender: AnyObject)
{
if self.emailTextField.text == "" || self.usernameTextField.text == "" || self.passwordTextField.text == ""

{
let alertController = UIAlertController(title: "Oops!", message: "Please enter a valid username, email & password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)

self.present(alertController, animated: true, completion: nil)
}
else
{
let data = UIImageJPEGRepresentation(self.userProfilePicture.image!, 0.8)

networkingService.signUp(email: emailTextField.text!, username: usernameTextField.text!, password: passwordTextField.text!, data: data as NSData!)
}
}


NetworkingService is the struct that I call.

Thank you for the help, I still trying to wrap my head around MVC.

Answer

Add a completion block to your model's signUp method.

func signUp(email: String, username: String, password: String, data: NSData, completion: Error? -> ())
{
    FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
        if error == nil {
            self.setUserInfo(user: user, username: username, password: password, data: data)
        }
        completion(error)
    })
}

Then you provide a closure that will be called when the user is/isn't created.

@IBAction func signupButton(_ sender: AnyObject) {

    guard
        let email = emailTextField.text,
        let username = usernameTextField.text,
        let password = passwordTextField.text,
        let image = userProfilePicture.image,
        let data = UIImageJPEGRepresentation(image, 0.8)
    where
        email != "" &&
        username != "" &&
        password != ""
    else {
        let alertController = UIAlertController(title: "Oops!", message: "Please enter a valid username, email & password", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
        present(alertController, animated: true, completion: nil)
    }

    networkingService.signUp(email: email, username: username, password: password, data: data) { error in
        if let error = error {
            print(error.localizedDescription)
        }
    }
}