Swift App Crash User Login

Hi,
I’m pretty new to coding, so this may be a dumb question. I’m trying to create a register and login page. I’ve been able to register users and have been able to log them in. However, when a wrong username or password is entered, the app crashes. Any help would be appreciated, I’m about ready to pull out all my hair.
I’ve provided the coding below. The first part is the registration (that’s working) and the second part is where I’m having issues. Thanks!

let backendless = Backendless.sharedInstance()
 let user: BackendlessUser = BackendlessUser()
 user.email = self.email.text
 user.password = self.password.text
 _ = backendless.userService.registering(user)
 print("User has been registered (SYNC): \registeredUser")
 
 
 self.activityIndicator.stopAnimating()
 UIApplication.sharedApplication().endIgnoringInteractionEvents()
    } else { let backendless = Backendless.sharedInstance() let user = backendless.userService.login(email.text, password: password.text) print("User has been logged in (SYNC): \(user)")

    self.activityIndicator.stopAnimating()
    UIApplication.sharedApplication().endIgnoringInteractionEvents()

    if user != nil {

    // Logged in

    } else {

    displayAlert(“Login Attempt Failed”, message: “Please retry email and password”)

    }
    }

    }

Add a try/catch block around your code. When the server returns an error (in this case the error is that the username/password are incorrect), an exception is thrown with the API you’re using.

Mark

I tried the code provided on the website, but it did’t work :frowning:

func loginUser() {
Types.tryblock({ () -> Void in 
let user = self.backendless.userService.login("email.text, password: password.text) 
print("User has been logged in (SYNC): \(user)") }, 
catchblock: { (exception) -> Void in 
print("Server reported an error: \(exception as! Fault)") }) }

Did you copy and paste that from XCode? The double quote in front of email.text seems out of place.

Sorry, that was a typo, the quote isn’t there

Do you see the following message in Xcode when the app runs?:

User has been logged in (SYNC)

I get that message, but only if the user’s been registered and the same email and password are used to login. If it’s an email or password not registered, the app crashes

Hi Josh,

Does this issue still reproduce?