【问题标题】:Why does AWSCognitoCredentialsProvider.getIdentityId().continueWith(block:) not run its block?为什么 AWSCognitoCredentialsProvider.getIdentityId().continueWith(block:) 不运行它的块?
【发布时间】:2019-10-13 11:58:50
【问题描述】:

我正在尝试获取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("***")

【问题讨论】:

    标签: ios swift block amazon-cognito


    【解决方案1】:

    由于您在 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

    【讨论】:

    • 我返回了 nil,但我仍然得到相同的结果。
    • 可能是因为您没有使用定义的队列吗?尝试将 continueWith: 替换为 continueWithExecutor:block: 并使用 AWSExecutor.defaultExecutor 作为 Executor 参数
    • 你为什么使用getIdentityId而不是credentials
    • 我正在使用文档中第 7 步中的示例代码:docs.amazonaws.cn/en_us/cognito/latest/developerguide/…
    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多