未调用AWS Cognito iOS登录委托方法'DidCompleteTestepWithError'

未调用AWS Cognito iOS登录委托方法'DidCompleteTestepWithError',ios,swift,amazon-web-services,aws-sdk,aws-cognito,Ios,Swift,Amazon Web Services,Aws Sdk,Aws Cognito,我正在我基于swift的iOS应用程序中实现AWS Cognito登录,但是不会触发委托方法didCompleteTestepWithError CognitoLoginViewController class CognitoLoginViewController: UIViewController, AWSCognitoIdentityPasswordAuthentication { @IBOutlet weak var txtEmail: UITextField! @IBO

我正在我基于swift的iOS应用程序中实现AWS Cognito登录,但是不会触发委托方法
didCompleteTestepWithError

CognitoLoginViewController

class CognitoLoginViewController: UIViewController, AWSCognitoIdentityPasswordAuthentication {

    @IBOutlet weak var txtEmail: UITextField!
    @IBOutlet weak var txtPassword: UITextField!

    var user: AWSCognitoIdentityUser?
    var pool: AWSCognitoIdentityUserPool?
    var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }

    //MARK: - IBAction
    @IBAction func login(_ sender: Any) {
        if ((self.txtEmail.text?.isEmpty)! || (self.txtPassword.text?.isEmpty)!) {
            let alertController = UIAlertController(title: "Missing information",
                                                    message: "Please enter a valid user name and password",
                                                    preferredStyle: .alert)
            let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
            alertController.addAction(retryAction)
            self.navigationController?.present(alertController, animated: true, completion: nil)
        } else {
            let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails(username: self.txtEmail.text!, password: self.txtPassword.text!)
            self.passwordAuthenticationCompletion?.set(result: authDetails)
        }
    }

    @IBAction func signup(_ sender: Any) {
        let vc = CognitoSignUpViewController(nibName: "CognitoSignUpViewController", bundle: nil)
        navigationController?.pushViewController(vc, animated: true)
    }

    @IBAction func forgotPassword(_ sender: Any) {
        let vc = CognitoForgotPasswordViewController(nibName: "CognitoForgotPasswordViewController", bundle: nil)
        navigationController?.pushViewController(vc, animated: true)
    }


    //MARK: - AWSCognitoIdentityPasswordAuthentication delegate methods
    public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
        self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
    }

    public func didCompleteStepWithError(_ error: Error?) {
        DispatchQueue.main.async {
            if let error = error as? NSError {
                let alertController = UIAlertController(title: error.userInfo["__type"] as? String,
                                                        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.txtEmail.text = nil
                //self.dismiss(animated: true, completion: nil)
            }
        }
    }
}
AppDelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var rootNavigationController : UINavigationController?
    var loginViewController: CognitoLoginViewController?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        navigationInit()
        awsCognitoInit()
        return true
    }

    func application(_ application: UIApplication,
                     open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return true
    }

    func awsCognitoInit(){
        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoIdentityUserPoolRegion,
                                                                identityPoolId: CognitoIdentityUserPoolId)
        let serviceConfiguration = AWSServiceConfiguration(region: .USEast1,
                                                           credentialsProvider: credentialsProvider)
        let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId,
                                                                        clientSecret: CognitoIdentityUserPoolAppClientSecret,
                                                                        poolId: CognitoIdentityUserPoolId)
        AWSLogger.default().logLevel = .verbose
        AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration
        AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)
    }

    func navigationInit() {
        self.rootNavigationController = UINavigationController()
        let loginViewController = LoginViewController()
        self.rootNavigationController!.pushViewController(loginViewController, animated: false)

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = rootNavigationController
        self.window!.makeKeyAndVisible()
    }

    //MARK: -
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        // Saves changes in the application's managed object context before the application terminates.
        self.saveContext()
    }    
}

然而,我能够使用注册,忘记密码和代码确认成功。此外,cognito配置值是正确的,因为上面提到的其他操作工作正常。

您是否确认实际调用了
getDetails
来设置
passwordAuthenticationCompletion
对象?通常,要触发身份验证流启动,您需要执行类似于
userPool.currentUser().getSession()
的操作,根据需要调用
getDetails
,或者如果活动会话或刷新令牌已缓存,则使用该操作。

您是对的,不会调用
getDetails
。我试图在视图控制器的
viewDidLoad
中调用
userPool.currentUser().getSession()
,但这也不会触发
getDetails
调用。我是否将
getSession
调用放置在正确的位置。您是否将用户池中的委托设置为实现AWSCognitoIdentityInteractiveAuthenticationDelegate的类?pool.delegate=…您好,请共享您的代码,AWS Cognito iOS登录委托方法
didCompleteTestepWithError
未调用
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var rootNavigationController : UINavigationController?
    var loginViewController: CognitoLoginViewController?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        navigationInit()
        awsCognitoInit()
        return true
    }

    func application(_ application: UIApplication,
                     open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return true
    }

    func awsCognitoInit(){
        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoIdentityUserPoolRegion,
                                                                identityPoolId: CognitoIdentityUserPoolId)
        let serviceConfiguration = AWSServiceConfiguration(region: .USEast1,
                                                           credentialsProvider: credentialsProvider)
        let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId,
                                                                        clientSecret: CognitoIdentityUserPoolAppClientSecret,
                                                                        poolId: CognitoIdentityUserPoolId)
        AWSLogger.default().logLevel = .verbose
        AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration
        AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)
    }

    func navigationInit() {
        self.rootNavigationController = UINavigationController()
        let loginViewController = LoginViewController()
        self.rootNavigationController!.pushViewController(loginViewController, animated: false)

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = rootNavigationController
        self.window!.makeKeyAndVisible()
    }

    //MARK: -
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        // Saves changes in the application's managed object context before the application terminates.
        self.saveContext()
    }    
}