Alex Alex - 5 months ago 13
iOS Question

Swift + Firebase simulated login error

iOS rookie here struggling through the early stages of an app that was initially led by a mentor. Issue at the moment occurs in the login view controller:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let myRootRef = Firebase(url:"https://xxxx.firebaseio.com")
if myRootRef.authData.uid != nil {
self.performSegueWithIdentifier("loginToGreet", sender: nil)

}


Error message beside the second to last line reads "Thread 1: EXC_BAD_INSTRUCTION and below reads "fatal error: unexpectedly found nil while unwrapping an Optional value(lldb)"

This was working when we initially put the project on the shelf. The next chunk of code, if it matters:

@IBAction func loginPress(sender: UIButton) {
let myRootRef = Firebase(url:"https://xxx.firebaseio.com")
myRootRef.authUser(self.patientLoginName.text, password:self.patientLoginPassword.text,
withCompletionBlock: { error, authData in
if error != nil {
// There was an error logging in to this account
print(error)
} else {
// We are now logged in
print(authData)
self.performSegueWithIdentifier("loginToGreet", sender: nil)
}
})

Answer

Try

  if myRootRef.authData != nil {
     self.performSegueWithIdentifier("loginToGreet", sender: nil)
  }

authData might be nil and you are checking for authData.uid. Since authData is nil it will crash at the uid access so change your error checking like above.

Comments