2

我正在尝试在使用 AWS Cognito 的 iOS 应用程序上实现响应 FORCE_CHANGE_PASSWORD 的功能。我使用了这个 Stack Overflow 问题,它引用了这个示例代码。现在,我的代码打开了一个视图控制器,就像它应该的那样;但是,一旦在该视图控制器上,我就无法让它做任何事情。在示例代码中,似乎当您想提交.set对 的实例调用的密码更改请求AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>时,但是当我这样做时,didCompleteNewPasswordStepWithError永远不会调用协议函数。有趣的是,另一个协议函数getNewPasswordDetails在之后很快被调用viewDidLoad,我不知道为什么。我相信在用户输入新密码等之前不应该调用它,并且应该响应.set但我可能是错的。

我的代码与示例代码和 SO 帖子完全相同,所以我不确定这里出了什么问题。

我的相关 AppDelegate 代码在这里:

extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {

    func startNewPasswordRequired() -> AWSCognitoIdentityNewPasswordRequired {

        //assume we are presenting from login vc cuz where else would we be presenting that from
        DispatchQueue.main.async {
            let presentVC = UIApplication.shared.keyWindow?.visibleViewController
            TransitionHelperFunctions.presentResetPasswordViewController(viewController: presentVC!)
            print(1)
        }

        var vcToReturn: ResetPasswordViewController?
        returnVC { (vc) in
            vcToReturn = vc
            print(2)
        }
        print(3)
        return vcToReturn!

    }

    //put this into its own func so we can call it on main thread
    func returnVC(completion: @escaping (ResetPasswordViewController) -> () ) {

        DispatchQueue.main.sync {
            let storyboard = UIStoryboard(name: "ResetPassword", bundle: nil)
            let resetVC = storyboard.instantiateViewController(withIdentifier: "ResetPasswordViewController") as? ResetPasswordViewController
            completion(resetVC!)
        }

    }
}

我相关的 ResetPasswordViewController 代码在这里:


class ResetPasswordViewController: UIViewController, UITextFieldDelegate {

    @IBAction func resetButtonPressed(_ sender: Any) {

        var userAttributes: [String:String] = [:]
        userAttributes["given_name"] = firstNameField.text!
        userAttributes["family_name"] = lastNameField.text!

        let details = AWSCognitoIdentityNewPasswordRequiredDetails(proposedPassword: self.passwordTextField.text!, userAttributes: userAttributes)
        self.newPasswordCompletion?.set(result: details)

    }

}

extension ResetPasswordViewController: AWSCognitoIdentityNewPasswordRequired {

    func getNewPasswordDetails(_ newPasswordRequiredInput: AWSCognitoIdentityNewPasswordRequiredInput, newPasswordRequiredCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>) {

        self.newPasswordCompletion = newPasswordRequiredCompletionSource

    }

    func didCompleteNewPasswordStepWithError(_ error: Error?) {
        DispatchQueue.main.async {
            if let error = error as? NSError {
                print("error")
                print(error)
            } else {
                // Handle success, in my case simply dismiss the view controller
                SCLAlertViewHelperFunctions.displaySuccessAlertView(timeoutValue: 5.0, title: "Success", subTitle: "You can now login with your new passowrd", colorStyle: Constants.UIntColors.emeraldColor, colorTextButton: Constants.UIntColors.whiteColor)

                self.dismiss(animated: true, completion: nil)
            }
        }
    }

}

非常感谢您提前提供的帮助,如果您需要更多信息,请告诉我。

4

0 回答 0