Swift & Parse - PFUser currentUser never equals nil

依然范特西╮ 提交于 2019-12-03 00:47:28
    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

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!

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.

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)

    }


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!