Swift 和 Parse - PFUser currentUser 永远不等于 nil

问题描述 投票:0回答:4

我正在使用 Xcode、Swift 和 Parse。当我尝试注销 PFUser 时,我从未得到 nil 返回。

在应用程序的这一部分中,viewController 只是显示一些登录按钮。其中一个按钮让用户进行注册。一种是发送用户更改详细信息,另一种是简单的注销。

注销时重要的两个代码是;

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

    }

}

一旦代码运行并注销,无论在何处读取 currentUser,我都会收到以下类型的响应,而不是 nil。下一个 ViewController 被执行,如果没有 usr 登录,这不应该发生。

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

我做错了什么还是这个标准? 如果正确的话如何返回没有用户登录?

谢谢

ios xcode swift parse-platform pfuser
4个回答
3
投票
    if PFUser.currentUser()!.username != nil
    {
        self.performSegueWithIdentifier("loginSegue", sender: self)
    }

上面的代码适用于登录问题。但我仍然遇到注销问题。在我调用 PFUser.logout() 后,PFUser.currentUser() 不会变为 nil。有什么帮助吗?

谢谢


3
投票

我一直在努力退出一段时间,我相信我终于破解了它!

无论我做什么,当我使用“PFUser.logOut()”时,永远不会将“PFUser.currentUser()”设置为nil,但它会将“PFUser.currentUser()!.username”设置为nil...

因此我使用了

var currentUser = PFUser.currentUser()!.username

作为全局变量来跟踪用户是否登录。

在我的登录/首页添加了

override func viewDidAppear(animated: Bool) {

    if currentUser != nil {

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

    }

}

最后在我使用的注销按钮上

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

    }

}

希望有帮助!


3
投票

尝试在 AppDelegate.swift 文件中注释掉以下代码行 -

PFUser.enableAutomaticUser()

enableAutomaticUser()
拨打
PFUser.logOut()
后,将会以匿名用户身份登录,匿名用户的用户名是
nil


1
投票

覆盖 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) } }
© www.soinside.com 2019 - 2024. All rights reserved.