首页 > 解决方案 > AWS Cognito 使用 Swift 登录后如何获得 IAM 权限?

问题描述

我使用 AWS Cognito 作为我用 Swift 编写的 iOS 应用程序的身份验证机制。用户成功登录后,我很难检索用户的访问令牌。我将在下面包含一些代码。我在这里问问题作为最后的手段。对于我来说,文档只是让我感到困惑和不一致。任何帮助将不胜感激。注:核心问题是登录后无法访问AWS资源

应用委托

// setup service configuration

let serviceConfiguration = AWSServiceConfiguration(region: CognitoIdentityUserPoolRegion, credentialsProvider: nil)

// create pool configuration
let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId, clientSecret: CognitoIdentityUserPoolAppClientSecret,
poolId: CognitoIdentityUserPoolId)

// initialize user pool client
AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)

// fetch the user pool client we initialized in above step
let pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)

登录视图控制器

 AWSMobileClient.default().signIn(username: username, password: password) { (signInResult, error) in

    if let error = error  {

         print("\(error.localizedDescription)")

    }
    else if let signInResult = signInResult {

       switch (signInResult.signInState) {

       case .signedIn:

           print("User is signed in.")

       default:

           print("Sign In needs info which is not yet supported.")
       }

   }
}

我已经看过的资源:

https://github.com/awslabs/aws-sdk-ios-samples/blob/master/CognitoYourUserPools-Sample/Swift/CognitoYourUserPoolsSample/SignInViewController.swift

https://aws-amplify.github.io/docs/ios/authentication

我很难过

标签: iosswiftamazon-web-servicesauthenticationamazon-cognito

解决方案


感谢您提出问题 Ryan,如果您还有其他问题,可能会更容易获得一些牵引力并通过在 Github Repo 中询问来改进文档(https://github.com/aws-amplify/aws -sdk-ios/issues),因为它在那里更活跃。

AWSMobileClient 使用该awsconfiguration.json文件。

您可以运行自动设置以查看其中放置的内容awsconfiguration.jsonhttps ://aws-amplify.github.io/docs/ios/authentication#automated-setup

如果自动设置不符合您的需求,您还可以手动制作awsconfiguration.json文件以供 AWSMobileClient 使用。

  1. 使用 Amplify CLI 配置和生成awsconfiguration.json文件。
{
        "IdentityManager": {
            "Default": {}
        },
        "CredentialsProvider": {
            "CognitoIdentity": {
                "Default": {
                    "PoolId": "XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab",
                    "Region": "XX-XXXX-X"
                }
            }
        },
        "CognitoUserPool": {
            "Default": {
                "PoolId": "XX-XXXX-X_abcd1234",
                "AppClientId": "XXXXXXXX",
                "AppClientSecret": "XXXXXXXXX",
                "Region": "XX-XXXX-X"
            }
        }
    }
  1. 将它放在您的项目中,添加到 Bundles 以供 AWSMobileClient 访问

  2. 在代码中,初始化 AWSMobileClient

AWSMobileClient.default().initialize { (userState, error) in
            if let userState = userState {
                print("UserState: \(userState.rawValue)")
            } else if let error = error {
                print("error: \(error.localizedDescription)")
            }
        }

推荐阅读