swift:在登录 View Controller 中未调用 AWSCognitoIdentityInteractiveAuthenticationDelegate 方法

标签 swift aws-userpools

我正在尝试使用 AWSCognito 用户池登录,但未调用 AWSCognitoIdentityInteractiveAuthenticationDelegate 方法。这是我的代码,我哪里做错了?

import UIKit
import AWSCognito
import AWSCognitoIdentityProvider

class LoginViewController: UIViewController,     AWSCognitoIdentityInteractiveAuthenticationDelegate {

@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
var user: AWSCognitoIdentityUser?

override func viewDidLoad() {
    super.viewDidLoad()

    loginButton.layer.cornerRadius = 10
    loginButton.layer.borderWidth = 1

    pool.delegate = self
}

@IBAction func loginTap(_ sender: Any) {
    if let email = emailTextField.text, let password = passwordTextField.text {
        if email.isValidEmail() {
            let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails.init(username: email, password: password)
            self.passwordAuthenticationCompletion?.set(result: authDetails)
            self.user?.getSession()
        } else {
            let alert = UIAlertController(title: "Alert", message: "Invalid Email or password", preferredStyle: UIAlertController.Style.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

@IBAction func cancelButtonTap(_ sender: Any) {
    dismiss(animated: true, completion: nil)
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        NotificationCenter.default.post(name: NSNotification.Name("goToPaymentPageVC"), object: nil)
    }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    emailTextField.resignFirstResponder()
    passwordTextField.resignFirstResponder()
}


//:- MARK: AWSCognitoIdentityInteractiveAuthenticationDelegate methods
func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
    self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
    DispatchQueue.main.async {
        if (self.emailTextField.text == nil) {
            self.emailTextField.text = authenticationInput.lastKnownUsername
        }
    }
}

func didCompleteStepWithError(_ error: Error?) {
    DispatchQueue.main.async {
        if let error = error as NSError? {
            let alertController = UIAlertController(title: "Error",
                                                    message: error.userInfo["message"] as? String,
                                                    preferredStyle: .alert)
            let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
            alertController.addAction(retryAction)

            self.present(alertController, animated: true, completion:  nil)
        } else {
            self.emailTextField.text = nil
            self.dismiss(animated: true, completion: nil)
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                NotificationCenter.default.post(name: NSNotification.Name("goToPaymentPageVC"), object: nil)
            }
        }
    }
}
}

预期结果:应调用 AWSCognitoIdentityInteractiveAuthenticationDelegate 方法并关闭 loginViewController。

最佳答案

这是修复的方式:

  1. LoginViewController 类符合 AWSCognitoIdentityPasswordAuthentication 并在同一类中添加以下函数

    func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication { 回归 self }

  2. 在调用 self.user?.getSession()

  3. 之前添加 pool.clearAll()

关于swift:在登录 View Controller 中未调用 AWSCognitoIdentityInteractiveAuthenticationDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54728532/

相关文章:

将变量传递到排序闭包的快速语法

ios - 如何在 user?.getDetails 调用后从 AWS Cognito 用户池获取特定属性

amazon-web-services - AWS CloudFormation 脚本失败 - Cognito 不允许使用您的电子邮件身份

amazon-web-services - Cognito 托管 UI 中的可选标识符不起作用

swift - UITableViewDataSource 方法不起作用

swift - 处理 "Could not cast value of type ' NSTaggedPointerString' 到 'NSNumber' ”

ios - 如果用于 'numberOfRowsIndexPath' .count 的数组为 0,则更改 TableView 的大小

ios - 为什么 Swift 2 不读取这个 iOS 文件?

amazon-web-services - 是否可以更改 AWS Cognito 托管 UI?