我设法找到的主要信息来源是GitHub 上的 open issue 。在阅读并找到 J.Toomey 在AWS 论坛上写的帖子后,我发现“AWSCognitoCredentialsProvider 是延迟加载的”。并且能够使用以下代码解决我的问题:
1) 实现了一个自定义 AWSIdentityProviderManager
import Foundation
import AWSCognito
class CustomAWSProvider:NSObject, AWSIdentityProviderManager{
var tokens : [NSString : NSString]?
init(tokens: [NSString : NSString]) {
self.tokens = tokens
print("tokens : ", self.tokens);
}
@objc func logins() -> AWSTask {
return AWSTask(result: tokens)
}
}
2) 使用此自定义提供程序如下:
let digitsConfiguration = DGTAuthenticationConfiguration(accountFields: .DefaultOptionMask)
Digits.sharedInstance().authenticateWithViewController(nil, configuration: digitsConfiguration) { session, error in
if session != nil {
let credentials = session.authToken + ";" + session.authTokenSecret
let customProvider = CustomAWSProvider.init(tokens: [AWSIdentityProviderDigits : credentials])
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USWest1, identityPoolId: cognitoIdentityPoolId , identityProviderManager: customProvider)
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
credentialsProvider.credentials().continueWithBlock { (task: AWSTask!) -> AnyObject! in
return nil
}.continueWithBlock { (task: AWSTask ) -> AnyObject in
return credentialsProvider.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in
if (task.result != nil) {
print("You have been successfully stored in Cognito")
}
if (task.error != nil) {
print(task.error!.localizedDescription)
}
if (task.exception != nil) {
print(task.exception!.description)
}
return nil
}
}
} else {
NSLog("Authentication error: %@", error!.localizedDescription)
}
}