为什么AWSCognitoCredentialsProvider.getIdentityId()。continueWith(block :)不运行其块?

时间:2019-05-28 01:32:11

标签: ios swift block amazon-cognito

我正在尝试获取AWSCognitoCredentialsProvider身份ID ,但是该屏蔽无法运行。

为什么它不运行,我应该检查什么以调查此问题?或有什么解决方案?

    override func viewDidLoad() {
        super.viewDidLoad()

        //setup service config
        let serviceConfiguration: AWSServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)

        //create a pool
        let configuration: AWSCognitoIdentityUserPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "***", clientSecret: "***", poolId: "us-east-1_***")

        AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: configuration, forKey: "***")
        pool = AWSCognitoIdentityUserPool(forKey: "***")

        pool.delegate = self

        self.user = self.pool.currentUser()
        print("user=", self.user as Any)
        print("user.isSignedIn=", user.isSignedIn)

        let credentialsProvider: AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "us-east-1:***", identityProviderManager: self.pool)
        let defaultServiceConfiguration: AWSServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
        AWSServiceManager.default()!.defaultServiceConfiguration = defaultServiceConfiguration

        let taskNSString: AWSTask<NSString> = credentialsProvider.getIdentityId()
        print("taskNSString.result=", taskNSString.result as Any)

        // Retrieve your Amazon Cognito ID
        credentialsProvider.getIdentityId().continueWith(block: {
            (task) -> AnyObject? in
            if (task.error != nil) {
                print("Error: " + task.error!.localizedDescription)
            }
            else {
                // the task result will contain the identity id
                let cognitoId = task.result!
                print("Cognito id: \(cognitoId)")
            }
            return task;
        })

    }

    extension ViewController: AWSCognitoIdentityPasswordAuthentication {

        public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
            print("getDetails(_:authenticationInput:passwordAuthenticationCompletionSource:)")
            self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
            DispatchQueue.main.async {
                if (self.userNameText == nil) {
                    self.userNameText = authenticationInput.lastKnownUsername
                    print("userNameText=", self.userNameText as Any)
                }

            }
        }

        func didCompleteStepWithError(_ error: Error?) {

            print("didCompleteStepWithError\n", error as Any)

        }


    }

    extension ViewController: AWSCognitoIdentityInteractiveAuthenticationDelegate {

        func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
            print("startPasswordAuthentication")
            return self
        }

    }

以下是调试窗口中的打印结果:

user= Optional(<AWSCognitoIdentityUser: 0x600002644ab0>)
user.isSignedIn= false
startPasswordAuthentication
getDetails(_:authenticationInput:passwordAuthenticationCompletionSource:)
taskNSString.result= nil
startPasswordAuthentication
getDetails(_:authenticationInput:passwordAuthenticationCompletionSource:)
userNameText= Optional("***")

1 个答案:

答案 0 :(得分:0)

由于您要在continueWith:中返回一个任务(名为任务A),因此直到任务(任务A)完成后,该块才会执行。

AWSTask文档 RETURN: A task that will be completed after block has run. If block returns a AWSTask, then the task returned from this method will not be completed until that task is completed.

TL:DR尝试在continueWith:中返回nil