Swift&Parse – PFUser currentUser永远不会等于零
我正在使用Xcode,Swift和Parse。 当我尝试注销一个PFUser时,我从来没有得到零的回报。
在这个应用程序的部分,viewController只是简单地显示一个loginbutton。一个发送用户注册。 一个发送用户改变细节,一个是简单的注销。
注销时所涉及的两个代码是:
@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读取,我得到以下types的响应,而不是零。 下一个ViewController被执行,这不应该没有一个usrlogin。
PFUser:0x37018fbc0,objectId:new,localId:local_3b5eb7453f9af5ed {}
我做错了什么或者是这个标准? 如果这是正确的,我怎样才能返回没有用户login?
谢谢
if PFUser.currentUser()!.username != nil { self.performSegueWithIdentifier("loginSegue", sender: self) }
上面的代码适用于login问题。 但我仍然有注销问题。 在我调用PFUser.logout()之后,PFUser.currentUser()不会变为零。 任何帮助?
谢谢
我一直在努力退出一段时间,我相信我终于破解了!
不pipe我做了什么,当我使用“PFUser.logOut()”永远不会设置“PFUser.currentUser()”为零,但它会设置“PFUser.currentUser()!.用户名”为零…
正因为如此,我用了
var currentUser = PFUser.currentUser()!.username
作为一个全局variables来跟踪用户是否login。
在我的login/首页上添加
override func viewDidAppear(animated: Bool) { if currentUser != nil { self.performSegueWithIdentifier("login", sender: self) } }
最后在我使用的注销button上
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 } }
我希望有帮助!
重写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) } }
尝试在AppDelegate.swift文件中注释以下代码行 –
PFUser.enableAutomaticUser()
一旦你调用PFUser.logOut(),enableAutomaticUser()将会login一个匿名用户,而匿名用户的用户名是nil。
- iOS中片段(android)的等效项
- 如何避免在iOS OpenGL ES应用程序的自动转换中出现短暂的延伸
- 如何在Swift 3中使用%作为后缀一元运算符来计算百分比并仍然可以使用%来模数?
- UITableViewCell中的约束animation启动不正确
- AFNetworking(AFHttpClient)离线模式不适用于NSURLRequestReturnCacheDataDontLoad策略
- FbConnect:同时发布在用户的墙上和新闻源中?
- 我的IOS上的Socket.io无法与websocket服务器握手
- 有一半的UILabel文字大胆,一半没有?
- Core Plot 1.0如何实现reloadData方法?