Swift & Parse - the current PFUser value is never null

I am using Xcode, Swift and Parse. When I try to exit PFUser, I never get a null return.

In this part of the application, the viewController simply shows multiple buttons that include one input. One sends the user for registration. One sends the user a change to the information, and one sends a simple logout.

Code for two that matters when logging out:

@IBAction func logout(sender: AnyObject) {

    PFUser.logOut()
    var currentUser = PFUser.currentUser()

    self.displayAlert("You are now logged out", error: "")

    println(currentUser!)
}

@IBAction func changeDetails(sender: AnyObject) {

    var currentUser = PFUser.currentUser()

    println(currentUser!)

        if currentUser != nil {

            let nextView30 = self.storyboard?.instantiateViewControllerWithIdentifier("changeDetails") as! changeUserDetailsViewController

           self.navigationController?.pushViewController(nextView30, animated: true)

    } else {

        self.displayAlert("Please log in", error: "")

    }

}

      

As soon as the code runs and I log out, wherever currentUser is read, I get the following response type, not null. The next ViewController action is in progress, and it shouldn't happen without a login.

PFUser: 0x37018fbc0, objectId: new, localId: local_3b5eb7453f9af5ed {}

Am I doing something wrong or is this the standard? If this is correct, how can I return any users?

thank

+3


source to share


4 answers


    if PFUser.currentUser()!.username != nil
    {
        self.performSegueWithIdentifier("loginSegue", sender: self)
    }

      

The above code worked for login. But I still have a logout problem. After I call PFUser.logout (), PFUser.currentUser () does not become null. Any help?



thank

+3


source


I have been struggling with logging out for a while now and I believe I finally hacked it!

No matter what I did, when I used "PFUser.logOut ()" would never set "PFUser.currentUser ()" to nil, but it did set "PFUser.currentUser () !. username" to nil .. ...

Because of this, I used

var currentUser = PFUser.currentUser()!.username

      

as a global variable to track, the user is logged in.



On my account / first page, I added

override func viewDidAppear(animated: Bool) {

    if currentUser != nil {

        self.performSegueWithIdentifier("login", sender: self)

    }

}

      

and finally on my logout button that I used

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "logout" {

        PFUser.logOut() //Log user out

        currentUser = PFUser.currentUser()!.username //Reset currentUser variable to nil

    }

}

      

I hope this helps!

+3


source


override func viewDidLoad (animated: Bool) {

     var currentUser = PFUser.currentUser()?.username



    if(currentUser != nil){


        var loginAlert: UIAlertController = UIAlertController(title: "Signup/ Login", message:"Please Signup or Login"  , preferredStyle: UIAlertControllerStyle.Alert)
        loginAlert.addTextFieldWithConfigurationHandler({
            textfield in

            textfield.placeholder = "Your Username"


        })

        loginAlert.addTextFieldWithConfigurationHandler({
            textfield in

            textfield.placeholder = "Your Password"
            textfield.secureTextEntry = true


        })



        loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler: {alertAction in



            let textFields: NSArray = loginAlert.textFields! as NSArray

            let usernameTextFields: UITextField = textFields.objectAtIndex(0) as! UITextField
            let passwordTextFields: UITextField = textFields.objectAtIndex(1) as! UITextField


            PFUser.logInWithUsernameInBackground(usernameTextFields.text as String!, password: passwordTextFields.text as String!){
                (loggedInuser: PFUser?, signupError: NSError?) -> Void in


                if((loggedInuser) != nil){
                    println("User logged in successfully")
                }else{
                println("User login failed")
                }
            }

        }))


        loginAlert.addAction(UIAlertAction(title: "SignUp", style: UIAlertActionStyle.Default, handler: {alertAction in



            let textFields: NSArray = loginAlert.textFields! as NSArray

            let usernameTextFields: UITextField = textFields.objectAtIndex(0) as! UITextField
            let passwordTextFields: UITextField = textFields.objectAtIndex(1) as! UITextField


            var sweeter : PFUser = PFUser()
            sweeter.username = usernameTextFields.text
            sweeter.password = passwordTextFields.text


            sweeter.signUpInBackgroundWithBlock {(sucess,error) -> Void in


                if !(error != nil){
                    println("sign up success")
                }else
                {
                    println("constant error string\(error)")
                }




            }




        }))

        self.presentViewController(loginAlert, animated: true, completion: nil)

    }


}

      

+1


source


Try commenting out the following line of code in your AppDelegate.swift file -

PFUser.enableAutomaticUser()

      

enableAutomaticUser () will log an anonymous user after calling PFUser.logOut () and the username for an anonymous user is nil.

+1


source







All Articles