我正在使用Alamofire进行基本身份验证,我有一个类可以存储到已登录的用户信息中。
我的问题是,当我签署一个用户然后签名并尝试登录其他用户时,我仍然会收到第一个用户退出的用户信息。
我的用户类:
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)
我也尝试了这个建议How to make basic auth with Alamofire?,它引发了#34;意外地在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)
}
}