3

I am using Xcode, Swift, and Parse. When I try and logout a PFUser, i never get a return of nil.

In this part of the app, the viewController is simply showing a few buttons one logs in. One sends the user to signup. One sends the user to change details, and one is a simple logout.

The code for the two that matter on logout is;

@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: "")

    }

}

Once the code runs and I logout, wherever the currentUser gets read I get the following type of response, not nil. The next ViewController is actioned, and this shouldn't happen without a usr logged in.

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

Am I doing something wrong or is this standard? If it is correct, how do I return no user logged in?

Thanks

Midaero
  • 65
  • 2
  • 5
  • 1
    Maybe you've enabled `automatic user`. Do you have something like this in your code: `PFUser.enableAutomaticUser()`? – Vasil Garov May 13 '15 at 14:18
  • I have checked for Auto User. It isn't there. Im logging the user in as PFUser.logInWithUsernameInBackground(username.text, password:password.text) { (user: PFUser?, signupError: NSError?) -> Void in This is running through a login button – Midaero May 13 '15 at 16:12
  • 1
    Did you try to check PFUser.currentUser().username != nil ? it works well for me. – Guy Kahlon May 13 '15 at 18:41
  • Guy, it would appear that you have solved the mystery. I changed var currentUser = PFUser.currentUser() to var currentUser = PFUser.currentUser()?.username and it returned nil my sincere thanks. – Midaero May 13 '15 at 19:08
  • It looks just as a hacky workaround, not a documented solution. They show in the [docs](https://parse.com/docs/ios/guide#users-current-user) you should check nil against currentUser object. How to be sure it will work in the future the same way? – Sergei Basharov Sep 14 '15 at 11:04

4 Answers4

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

The above code worked for the login issue. But i still have the logout issue. After I call PFUser.logout(), PFUser.currentUser() is not becoming nil. Any help?

Thanks

sfbayman
  • 1,067
  • 2
  • 9
  • 22
  • 1
    The following code worked for me . PFUser.logOutInBackgroundWithBlock() { (error: NSError?) -> Void in if error != nil { print("logout fail") print(error) } else { print("logout success") } } – sfbayman Jul 15 '15 at 04:26
  • Perfect sfbayman , I had issues with that line of code. it works good now. – Roberto Aug 29 '15 at 18:57
3

I've been struggling with logging out for a little while and I believe I have finally cracked it!

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

Because of this I used

var currentUser = PFUser.currentUser()!.username

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

On my login/first page I added

override func viewDidAppear(animated: Bool) {

    if currentUser != nil {

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

    }

}

and finally on my logout button 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 that helps!

Kitson
  • 1,153
  • 9
  • 22
3

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

PFUser.enableAutomaticUser()

enableAutomaticUser() will log in an anonymous user once you call PFUser.logOut(), and the username for an anonymous user is nil.

dev27
  • 605
  • 1
  • 7
  • 15
1

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)

    }


}
  • Xcode update made several changedsand those are not reflected in Parse Framework yet. So it better to typecast/format the params as per the functions given parse in framework. Go through the entire code to see the changes in login and signup functions – Maxymus Boopathy May 20 '15 at 07:26
  • Thank you Maxymus, I am reviewing the code today, as I have changed the way I log users in and out. Your help is highly appreciated. – Midaero May 20 '15 at 09:20