1

我正在尝试使用 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。

4

2 回答 2

2

这就是它的固定方式:

  1. 使类符合LoginViewControllerAWSCognitoIdentityPasswordAuthentication在同一类中添加以下功能

    func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication { return self }

  2. pool.clearAll()调用前添加self.user?.getSession()

于 2019-02-18T15:46:44.407 回答
0

你需要打电话

pool.clearAll()

在您执行登录程序之前

let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails.init(username: email, password: password)
        self.passwordAuthenticationCompletion?.set(result: authDetails)
        self.user?.getSession()

如果您之前已经在该特定设备/模拟器上登录过,AWS 开发工具包不会调用相应的方法。

于 2019-02-18T15:42:30.617 回答