ios - 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://aws-amplify.github.io/docs/ios/authentication
我很难过
解决方案
感谢您提出问题 Ryan,如果您还有其他问题,可能会更容易获得一些牵引力并通过在 Github Repo 中询问来改进文档(https://github.com/aws-amplify/aws -sdk-ios/issues),因为它在那里更活跃。
AWSMobileClient 使用该awsconfiguration.json
文件。
您可以运行自动设置以查看其中放置的内容awsconfiguration.json
:https ://aws-amplify.github.io/docs/ios/authentication#automated-setup
如果自动设置不符合您的需求,您还可以手动制作awsconfiguration.json
文件以供 AWSMobileClient 使用。
- 使用 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"
}
}
}
将它放在您的项目中,添加到 Bundles 以供 AWSMobileClient 访问
在代码中,初始化 AWSMobileClient
AWSMobileClient.default().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState.rawValue)")
} else if let error = error {
print("error: \(error.localizedDescription)")
}
}
推荐阅读
- javascript - How to handle live data as sequential promises
- c# - ASP.NET Core 2.0 Web Api Error
- javascript - JS: weird object comparison behavior
- php - Phalcon 登录失败 (getDI)
- javascript - How to attach multiple data to a jquery object using .data() in a plugin
- python - Apply GDB FrameDecorator when stepping
- algorithm - EC scalar multiplication with Strauss-Shamir method
- java - 如何优化我的遗传算法结果
- javascript - JavaScript 的 ES6 导入如何处理循环依赖?
- bash - 在退出由相同 bash 脚本调用的程序时触发的脚本命令