我已经能够在单个 iOS 设备上注册、确认和同步用户数据集,但在尝试在其他设备上同步时遇到了问题。我正在努力弄清楚为什么会这样。在注册过程中,我同步了数据集,我可以在联合身份身份浏览器中看到它。但是在其他设备上同步时,我无法从我的数据集中读取数据。用户信息是否存储在设备本地?我的 Cognito 同步逻辑不正确吗?我应该用另一种方法来解决这个问题吗?下面是我在登录时同步用户数据的代码。提前谢谢..我真的很困惑如何解决这个问题。
func login(usernameText : String, passwordText : String){
user = self.pool!.getUser(usernameText)
var userString = ""
print("Login Current thread \(Thread.current)")
user?.getSession(usernameText, password: passwordText, validationData: nil).continue({ task in
DispatchQueue.main.async {
print(" Session Current thread \(Thread.current)")
if let err = task.error { // some sort of error
print("LOGIN FAILED")
print(err)
self.userConfirmed = false
//print(err.userInfo["message"] as! String)
}
else { //Successful login!
// this gets our token from the User Pool
let ret = task.result! as AWSCognitoIdentityUserSession
let myToken = ret.idToken?.tokenString;
let customcedentialProvider = AWSCustomIdentityProvider(tokens: [AWSCustomIdentityProvider.CognitoTokenKey : myToken!])
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoConstants.COGNITO_REGIONTYPE, identityPoolId: CognitoConstants.COGNITO_IDENTITY_POOL_ID, identityProviderManager: customcedentialProvider)
let configuration = AWSServiceConfiguration(region: CognitoConstants.COGNITO_REGIONTYPE, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
// wipe cached creds
credentialsProvider.clearKeychain()
credentialsProvider.clearCredentials()
// hit it
credentialsProvider.getIdentityId().continue({ (task: AWSTask!) -> AnyObject! in
DispatchQueue.main.async {
print("Current thread \(Thread.current)")
if (task.error != nil) {
print("Error: ")
} else {
print(credentialsProvider.identityId!)
// the task result will contain the identity id
let credentialsProviderUser = AWSCognitoCredentialsProvider(regionType:.usEast1,identityPoolId:credentialsProvider.identityId!)
let configurationUser = AWSServiceConfiguration(region:.usEast1, credentialsProvider:credentialsProviderUser)
AWSServiceManager.default().defaultServiceConfiguration = configurationUser
// Initialize the Cognito Sync client
let syncClient = AWSCognito.default()
// Get User Dataset
let dataset = syncClient?.openOrCreateDataset("myDataset")
print(dataset?.size() as Any)
let userData = dataset?.getAll()
for (key,value) in userData!{ if "\(value)".characters.count > 0 {userString.append("| Key: \(key) Value: \(value) ");print("| Key: \(key) Value: \(value) ")}
}
}
let defaults = UserDefaults.standard
defaults.setValue(userString, forKey: defaultsKeys.keyOne)
defaults.setValue(passwordText, forKey: defaultsKeys.keyTwo)
defaults.synchronize()
}
return nil
})
}}
return nil
})
}
// Class for custom tokens
class AWSCustomIdentityProvider: NSObject, AWSIdentityProviderManager
{
static let FacebookTokenKey = "graph.facebook.com"
static let GoogleTokenKey = "accounts.google.com"
static let TwitterTokenKey = "api.twitter.com"
static let CognitoTokenKey = "cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXXXXXX"
var tokens: NSDictionary = [String : String]() as NSDictionary
init(tokens: [String : String]) {
self.tokens = tokens as NSDictionary
}
func logins() -> AWSTask<NSDictionary> { // AWSIdentityProviderManager delegate method
return AWSTask(result: tokens)
}
}
Best Answer-推荐答案 strong>
我怀疑您并没有真正获得身份验证(登录次数为 0),并且您看到的同步与未经身份验证的身份相关联。
我怀疑这是因为您缺少对凭据的调用。仅获取身份 ID 是不够的,如果您从不调用凭据,那么......凭据提供者从不调用您 logins(),如果他从不调用 logins(),那么他永远不会看到登录已完成,因此他永远不会增加登录计数(这就是为什么您可能会在身份上的 identityBrowser 中看到 0 个登录的原因)。这意味着您使用的是未经身份验证的身份。
如果您的登录次数为 0,则您未通过身份验证。
这个答案可能对你也有帮助。
multiple devices cognito ios login
从您的问题中不清楚您是如何为 google 和 twitter 等生成 token 的,但我应该指出,用户池能够返回登录信息。
关于ios - 无法跨多个 iOS 设备同步 Amazon Cognito 数据集,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/40952065/
|