Ios 注销用户和登录新用户
我正在使用Alamofire进行基本身份验证,我有一个类来存储已登录的用户信息 我的问题是,当我登录一个用户,然后注销他们,并尝试登录其他用户时,我仍然会得到第一个已注销的用户信息 我的用户类:Ios 注销用户和登录新用户,ios,swift,basic-authentication,alamofire,Ios,Swift,Basic Authentication,Alamofire,我正在使用Alamofire进行基本身份验证,我有一个类来存储已登录的用户信息 我的问题是,当我登录一个用户,然后注销他们,并尝试登录其他用户时,我仍然会得到第一个已注销的用户信息 我的用户类: class User { var userJSON: [String:AnyObject]? } let sharedUser = User() Alamofire.request(.GET, "https://gyminyapp.azurewebsites.net/api/User").a
class User {
var userJSON: [String:AnyObject]?
}
let sharedUser = User()
Alamofire.request(.GET, "https://gyminyapp.azurewebsites.net/api/User").authenticate(user: emailTextField.text!, password: passwordTextField.text!).responseJSON {
json in
let responseJSON = json.result.value as? [String: AnyObject]
// print("JSON: \(responseJSON)")
if responseJSON!["message"] != nil {
let errorMessage = responseJSON!["message"] as! String
alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .Alert)
alert?.addAction(okButton)
alert!.view.tintColor = BarItems.greenTintColor
self.presentViewController(alert!, animated: true, completion: nil)
} else {
if responseJSON != nil {
sharedUser.userJSON = responseJSON
}
self.performSegueWithIdentifier("tabSegue", sender: nil)
}
}
sharedUser.userJSON = nil
// Return user to sign in screen
self.dismissViewControllerAnimated(true, completion: nil)
身份验证:
class User {
var userJSON: [String:AnyObject]?
}
let sharedUser = User()
Alamofire.request(.GET, "https://gyminyapp.azurewebsites.net/api/User").authenticate(user: emailTextField.text!, password: passwordTextField.text!).responseJSON {
json in
let responseJSON = json.result.value as? [String: AnyObject]
// print("JSON: \(responseJSON)")
if responseJSON!["message"] != nil {
let errorMessage = responseJSON!["message"] as! String
alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .Alert)
alert?.addAction(okButton)
alert!.view.tintColor = BarItems.greenTintColor
self.presentViewController(alert!, animated: true, completion: nil)
} else {
if responseJSON != nil {
sharedUser.userJSON = responseJSON
}
self.performSegueWithIdentifier("tabSegue", sender: nil)
}
}
sharedUser.userJSON = nil
// Return user to sign in screen
self.dismissViewControllerAnimated(true, completion: nil)
注销:
class User {
var userJSON: [String:AnyObject]?
}
let sharedUser = User()
Alamofire.request(.GET, "https://gyminyapp.azurewebsites.net/api/User").authenticate(user: emailTextField.text!, password: passwordTextField.text!).responseJSON {
json in
let responseJSON = json.result.value as? [String: AnyObject]
// print("JSON: \(responseJSON)")
if responseJSON!["message"] != nil {
let errorMessage = responseJSON!["message"] as! String
alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .Alert)
alert?.addAction(okButton)
alert!.view.tintColor = BarItems.greenTintColor
self.presentViewController(alert!, animated: true, completion: nil)
} else {
if responseJSON != nil {
sharedUser.userJSON = responseJSON
}
self.performSegueWithIdentifier("tabSegue", sender: nil)
}
}
sharedUser.userJSON = nil
// Return user to sign in screen
self.dismissViewControllerAnimated(true, completion: nil)
我也尝试过这个建议,它抛出“在if self.responseJSON![“message”!=nil
let user = emailTextField.text
let password = passwordTextField.text
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.GET, "https://gyminyapp.azurewebsites.net/api/User", headers: headers)
.responseJSON {
json in
self.responseJSON = json.result.value as? [String: AnyObject]
print("JSON: \(self.responseJSON)")
if self.responseJSON!["message"] != nil {
let errorMessage = self.responseJSON!["message"] as! String
alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .Alert)
alert?.addAction(okButton)
alert!.view.tintColor = BarItems.greenTintColor
self.presentViewController(alert!, animated: true, completion: nil)
} else {
if self.responseJSON != nil {
sharedUser.userJSON = self.responseJSON
print(sharedUser.userJSON)
}
self.defaults.setObject(self.emailTextField.text!, forKey: "emailAddress")
self.defaults.setObject(self.passwordTextField.text!, forKey: "password")
self.performSegueWithIdentifier("tabSegue", sender: nil)
}
}
您应该添加调试日志,可能您没有传递新的电子邮件和pass。可能创建的共享用户对象在每个类中都不相同。我刚刚添加了打印(sharedUser.userJSON)到视图,将在我的登录控制器和身份验证成功时显示。登录前和注销后为零。//打印(“JSON:(responseJSON)”)您将其取消注释。如果您取消注释..它是否为不同的登录显示不同的用户详细信息?否。它显示相同的用户详细信息。您是否可以参加聊天?您应该添加调试日志,可能您没有传递新的电子邮件和密码。可能每个classI中创建的共享用户对象不同。我刚刚添加了打印(sharedUser.userJSON)查看将在我的登录控制器中以及身份验证成功时显示。登录前和注销后均为零。//打印(“JSON:(responseJSON)”)可以取消注释。如果取消注释..是否为不同的登录显示不同的用户详细信息?否。它显示相同的用户详细信息。您能来聊天吗