我正在使用AWS Cognito作为我在Swift中编写的iOS应用程序的身份验证机制。一旦用户成功登录,我将很难检索用户的访问令牌。我将在下面提供一些代码。作为最后的解决方法,我在这里提出问题。该文档只是让我难以理解且前后矛盾。任何帮助将不胜感激。注意:核心问题是登录后无法访问任何AWS资源
AppDelegate
// 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)
SignInViewController
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
如果自动设置不符合您的需求,您也可以手动制作要由AWSMobileClient使用的awsconfiguration.json
文件。
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)")
}
}