Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用旧凭据的Alamofire身份验证_Ios_Alamofire - Fatal编程技术网

Ios 使用旧凭据的Alamofire身份验证

Ios 使用旧凭据的Alamofire身份验证,ios,alamofire,Ios,Alamofire,我正在使用Alamofire登录我的应用程序。用户名和密码来自文本字段。我可以很好地登录,但如果我注销并返回到登录屏幕,则随后的每次登录尝试都会使用原始凭据。例如,如果我输入'test@test.com'和'密码',我可以进入,但如果我退出并进入'test2@test.com'和'test2password',它使用'test@test.com'. 此外,如果我输入了错误的凭据,即使在我输入了正确的凭据之后,它也会始终说它们不正确。让它接受另一组凭据的唯一方法是强制关闭应用程序并重新打开它。另一

我正在使用Alamofire登录我的应用程序。用户名和密码来自文本字段。我可以很好地登录,但如果我注销并返回到登录屏幕,则随后的每次登录尝试都会使用原始凭据。例如,如果我输入'test@test.com'和'密码',我可以进入,但如果我退出并进入'test2@test.com'和'test2password',它使用'test@test.com'. 此外,如果我输入了错误的凭据,即使在我输入了正确的凭据之后,它也会始终说它们不正确。让它接受另一组凭据的唯一方法是强制关闭应用程序并重新打开它。另一方面,登录后对其他端点的后续调用都需要用户凭据。一旦我登录,所有这些都可以正常工作,但是当我使用授权头而不是Alamofire authenticate方法解决登录问题时,我随后的调用就不起作用了

下面是我如何尝试登录,以便所有后续呼叫都能正常工作,但这会导致每次都使用第一组凭据,直到我强制关闭应用程序

Alamofire.request("https://example.com/signin/", method: .get).authenticate(user: userName, password: password).responseJSON { response in
                if response.result.value != nil {
                    let results = response.result.value as? [[String: AnyObject]]
                    if results!.count > 0 {
                        if let dictionary = results?[0] {
                            if let userEmail = dictionary["email"] {
                                print("Signed in with: \(userEmail)")
                                sharedUser.userJSON = JSON(response.result.value!)
                                sharedUser.userEmail = self.usernameField.text!
                                sharedUser.userPassword = self.passwordField.text!
                                DispatchQueue.main.async {
                                    self.performSegue(withIdentifier: "signInSegue", sender: nil)
                                }
                            }
                        }
                    } else {
                        DispatchQueue.main.async {
                            let failedSignInAlert = UIAlertController(title: "Invalid Email or Password", message: "The information you entered is incorrect. Please verify you have the correct information and try again.", preferredStyle: .alert)
                            let failedAction = UIAlertAction(title: "OK", style: .default, handler: { (action) in
                                let cookieStorage = HTTPCookieStorage.shared
                                for cookie in cookieStorage.cookies! {
                                    cookieStorage.deleteCookie(cookie)
                                }
                                let urlCache = URLCache.shared
                                urlCache.removeAllCachedResponses()
                                self.dismiss(animated: true, completion: nil)
                            })
                            failedSignInAlert.addAction(failedAction)
                            self.present(failedSignInAlert, animated: true, completion: nil)
                        }
                    }
                } else {
                    print("SIGN IN FAILED!")
                }

                DispatchQueue.main.async {
                    self.loadingIndicator.stopAnimating()
                    self.signInButton.isEnabled = true
                }
            }

默认情况下,
.authenticate
使用
URLCredential.Storage
.forSession
,这意味着凭证将自动用于会话中的所有身份验证挑战,Alamofire没有机会提供其新的
URLCredential
。传递
的值。无
可能会解决您的问题

.authenticate(用户:用户名,密码:密码,持久性:。无)