Calling Auth.auth().signOut() automatically dismisses the ViewController - ios

The current view controller gets dismissed as soon as I call the signOut method by pressing the button. The signing out process however is successful. (I'm using the FirebaseAuth SDK.)
Here is my code:
#IBAction func logoutPressed(_ sender: Any) {
do {
try Auth.auth().signOut()
} catch let signOutError as NSError {
showAlertSaying(
title: "Fehler beim ausloggen",
message: "Ein Fehler ist aufgetreten",
view: self
)
print ("Error signing out: %#", signOutError)
}
}
Is there any way to prevent that the view controller gets dismissed?

It appears that your storyboard button is linked to another action in addition to this
#IBAction func logoutPressed(_ sender: Any) {
which has a dismiss / pop action
This scenario happens when you copy elements in storyboard

Related

I am getting the error warning of "whose view is not in the window hierarchy!" upon signing out?

I am using Firebase authentication in swift iOS. When signing out, I am getting the following error:
whose view is not in the window hierarchy!
Below is the button action that is being performed, what should I follow, ignore it or in future it will give me some major issues?
#IBAction func signOut(_ sender: Any) {
try! Auth.auth().signOut()
self.performSegue(withIdentifier: "toLoginScreen1", sender: self)
}
}

Segue Still Goes Through Despite Conditional Statement

I'm new to swift and Xcode. I creating a user registration view controller and I'm using Firebase as my DB. Here is what I've written:
#IBAction func registerButton(_ sender: Any) {
//Register user functions
Auth.auth().createUser(withEmail: emailField.text! , password: passwordField.text!) { (user, error) in
if error != nil {
print (error!)
} else {
//success
print ("Registration successful")
self.performSegue(withIdentifier: "registerToList", sender: self)
}
}
}
When a user inputs data that is incorrectly formatted, such as blank fields or improperly formatted email, my console prints out the error. However, the user is still able to bypass the registration view controller and move on to the next screen. Why is that?
I also want to note that I'm using a Show segue - is this the issue? I can't use a push segue since it has been deprecated.

Swift Firebase login

I'm having a problem where every time I enter the right credentials, it brings me to one view controller then opens up the same view controller again even though I only have the login viewer controller linked to one view controller. If I don't enter the right credentials it still brings me into the linked view controller. Here is the code.
EDIT: Using a push segue(show)
#IBAction func loginTapped(_ sender: Any) {
if let Email = userEmail.text, let Pass = userPassword.text{
Auth.auth().signIn(withEmail: Email, password: Pass, completion: { (user, error) in
if error != nil{
print("incorrect")
}
else{
if error == nil{
self.performSegue(withIdentifier: "loginPage", sender: self)
print("correct")
}
}
})
}
}
I don't know if you've fixed your problem, but check your storyboard. Sounds like you have a segue connected from the button to the next ViewController which would result in pressing the button and it'll always push that ViewController.
To do this easily just see if you have a segue connected from the button to your destination ViewController in your MainStoryboard.

Prevent Segue Transition in Login Swift/Parse

I have a login view controller where it should prevent the user from transition to the next view controller via a segue called "toMasterTab". I think the logic might be wrong - if the user entered the correct credentials and is not empty, it transitions fine, but if the user entered no credentials (nil) and entered the wrong credentials, then it should prevent the segue. So far, I can only get the UIAlertView to pop up, but other than that, I can't solve this...
#IBAction func loginButton(sender: AnyObject) {
let RedPlanetUser = RPUsername.text
let RedPlanetUserPassword = RPUserPassword.text
PFUser.logInWithUsernameInBackground(RedPlanetUser!, password: RedPlanetUserPassword!) {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
// Do stuff after successful login
self.performSegueWithIdentifier("toMasterTab", sender: self)
print("User logged in successfully")
} else {
// Login failed
print("User log in failed")
// Present alert
var alert = UIAlertView(title: "Login Failed",
message: "The username and password do not match.",
delegate: self,
cancelButtonTitle: "Try Again")
alert.show()
func shouldPerformSegueWithIdentifier(identifier: String!, object: AnyObject) -> Bool {
let identifier = "toMasterTab"
// prevent segue
return false
}
}
}
}
I believe you should be overriding the
override func shouldPerformSegueWithIdentifier
The problem was that the segue was connected to the button, so it automatically performed the segue even when the conditions were NOT met. I connected the segue from VC1 to VC2 and used the following code when the conditions were met, and didn't call the segue when the conditions were erroneous:
self.performSegueWithIdentifier("toMasterTab", sender: self)

Facebook Sign In Segue Loads and Then Returns to Sign In Screen

So I have created a Sign In / Sign Up function using Parse and Swift.
After successful Facebook login the view controller I want to see pops up briefly but then the SignIn view controller comes back onto the screen. I want the user to be logged in and have access to the App. Why is this?
Below is my code for both view controllers. I have a storyboard with segues setup as shown below.
fbSignIn - Facebook Sign In Segue
goSignIn - Sign In Page Segue
Main View Controller (The View Controller I want to go to after Facebook Login)
class MainViewController: UIViewController {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
if PFUser.currentUser() != nil {
} else {
// take user to SignInViewController through a custom segue
self.performSegueWithIdentifier("goSignIn", sender: self)
}
}
#IBAction func logOutUser(sender: UIButton) {
PFUser.logOut()
self.performSegueWithIdentifier("goSignIn", sender: self)
}
Sign In View Controller (Sign In and Facebook Sign In Button)
#IBAction func didTapFacebookConnect(sender: AnyObject) {
let permissions = [ "public_profile", "email", "user_friends" ]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
print ("User signed up and logged in through Facebook!")
self.performSegueWithIdentifier("fbSignIn", sender: self)
} else {
print ("User logged in through Facebook!")
self.performSegueWithIdentifier("fbSignIn", sender: self)
}
} else {
print ("Uh oh. The user cancelled the Facebook login.")
self.performSegueWithIdentifier("fbSignIn", sender: self)
}
You Segue to your Facebook login page on a viewDidAppear method in your main viewcontroller. The behaviour you describe suggests that PFUser.currentUser() is nil so self.performSegueWithIdentifier("goSignIn", sender: self) is executed in viewDidAppear when your main viewcontroller appears and you immediately end up back in the sign in page.

Resources