【问题标题】:Swift & Parse - PFUser currentUser never equals nilSwift & Parse - PFUser currentUser 从不等于 nil
【发布时间】:2015-05-13 13:47:21
【问题描述】:

我正在使用 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 { }

我做错了什么还是这是标准? 如果正确,如何返回无用户登录?

谢谢

【问题讨论】:

  • 也许你已经启用了automatic user。你的代码中有这样的东西吗:PFUser.enableAutomaticUser()
  • 我已经检查了自动用户。它不在那里。我将用户登录为 PFUser.logInWithUsernameInBackground(username.text, password:password.text) { (user: PFUser?, signupError: NSError?) -> Void in This is running through a login button
  • 您是否尝试检查 PFUser.currentUser().username != nil ?它对我很有效。
  • 伙计,看来你已经解开了谜团。我将 var currentUser = PFUser.currentUser() 更改为 var currentUser = PFUser.currentUser()?.username 并返回 nil 我真诚的感谢。
  • 它看起来只是一个 hacky 解决方法,而不是文档化的解决方案。它们显示在docs 中,您应该针对 currentUser 对象检查 nil。如何确保它在未来以同样的方式工作?

标签: ios xcode swift parse-platform pfuser


【解决方案1】:
    if PFUser.currentUser()!.username != nil
    {
        self.performSegueWithIdentifier("loginSegue", sender: self)
    }

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

谢谢

【讨论】:

  • 以下代码对我有用。 PFUser.logOutInBackgroundWithBlock() { (error: NSError?) -> Void in if error != nil { print("logout fail") print(error) } else { print("logout success") } }
  • 完美的 sfbayman ,我对那行代码有疑问。现在效果很好。
【解决方案2】:

我一直在努力退出登录,我相信我终于破解了它!

无论我做什么,当我使用“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。

    【讨论】:

      【解决方案4】:

      覆盖 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 更新进行了一些更改,这些更改尚未反映在 Parse Framework 中。因此,最好根据框架中给出的函数对参数进行类型转换/格式化。浏览整个代码以查看登录和注册功能的变化
      • 谢谢 Maxymus,我今天正在审查代码,因为我改变了登录和注销用户的方式。非常感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多