Ios 在AWS Cognito使用Swift登录后,如何获得IAM权限?
我正在使用AWS Cognito作为我在Swift中编写的iOS应用程序的身份验证机制。一旦用户成功登录,我很难检索用户的访问令牌。我将在下面包含一些代码。作为最后手段,我在这里提问。文档对我来说是混乱和不一致的。任何帮助都将不胜感激。注意:核心问题是登录后,无法访问AWS资源 AppDelegateIos 在AWS Cognito使用Swift登录后,如何获得IAM权限?,ios,swift,amazon-web-services,authentication,amazon-cognito,Ios,Swift,Amazon Web Services,Authentication,Amazon Cognito,我正在使用AWS Cognito作为我在Swift中编写的iOS应用程序的身份验证机制。一旦用户成功登录,我很难检索用户的访问令牌。我将在下面包含一些代码。作为最后手段,我在这里提问。文档对我来说是混乱和不一致的。任何帮助都将不胜感激。注意:核心问题是登录后,无法访问AWS资源 AppDelegate // setup service configuration let serviceConfiguration = AWSServiceConfiguration(region: Cognito
// 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.")
}
}
}
我已经看过的资源:
我被难住了谢谢你问我这个问题,Ryan,如果你还有问题,在Github Repo()中询问它可能会更容易获得一些信息,并改进文档,因为它在那里更活跃 AWSMobileClient使用
awsconfiguration.json
文件
您可以运行自动设置以查看放置在awsconfiguration.json中的内容:
如果自动设置不符合您的需要,您还可以手动创建AWSMobileClient使用的awsconfiguration.json
文件
使用Amplify CLI设置并生成awsconfiguration.json
文件
将其放在项目中,添加到包中,以便AWSMobileClient访问
在代码中,初始化AWSMobileClient
感谢您提出这个问题,Ryan,如果您还有其他问题,可以通过在Github Repo()中提问来更轻松地了解它,并改进文档,因为它在那里更活跃
AWSMobileClient使用awsconfiguration.json
文件
您可以运行自动设置以查看放置在awsconfiguration.json中的内容:
如果自动设置不符合您的需要,您还可以手动创建AWSMobileClient使用的awsconfiguration.json
文件
使用Amplify CLI设置并生成awsconfiguration.json
文件
将其放在项目中,添加到包中,以便AWSMobileClient访问
在代码中,初始化AWSMobileClient
{
"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"
}
}
}
AWSMobileClient.default().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState.rawValue)")
} else if let error = error {
print("error: \(error.localizedDescription)")
}
}